This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function hasDuplicates(array) { | |
return (new Set(array)).size !== array.length; | |
} | |
console.log(hasDuplicates(["M", "A", "S", "A", "U", "T", "T"])); // --> true | |
console.log(hasDuplicates(["M", "A", "R", "E", "K"])); // --> false | |
// https://stackoverflow.com/questions/7376598/in-javascript-how-do-i-check-if-an-array-has-duplicate-values |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
console.log(["M", "A", "R", "E", "K"].includes("R")); // --> true | |
console.log(["M", "A", "R", "E", "K"].includes("S")); // --> false |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Using Sets | |
function removeDupes(arr) { | |
return [...new Set(arr)]; | |
} | |
// Using filter( ) | |
function removeDupes1(arr) { | |
return arr.filter((v,i) => arr.indexOf(v) === i) | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var longestPalindrome = function(s) { | |
if (isPalindrome(s)) { | |
return s; | |
} | |
var maxP = s[0]; | |
for (var i=0; i < s.length; i++) { | |
var curr = s[i]; | |
for (var j= i + 1; j < s.length; j++) { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
String.prototype.count=function(s1) { | |
return (this.length - this.replace(new RegExp(s1,"g"), '').length) / s1.length; | |
} | |
console.log("Marek Sautter".count("e")); // --> 2 | |
console.log("I got a letter from Marek Sautter".count("tt")); // --> 2 | |
// https://stackoverflow.com/questions/881085/count-the-number-of-occurrences-of-a-character-in-a-string-in-javascript |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function getLetters(words) { | |
var words = (typeof words == 'string') ? words : '', | |
count = re => (words.match(re) || []).length, | |
vowels = count(/[aeiou]/ig), | |
consonants = count(/[bcdfghjklmnpqrstvxzwy]/ig); | |
return {vowels, consonants}; | |
} | |
console.log(getLetters("Marek Sautter")); // --> { vowels: 5, consonants: 7 } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function checkNum(str) { | |
return /^\d+$/.test(str) | |
} | |
console.log(checkNum("8675309")); // --> true | |
console.log(checkNum("867S309")); // --> false | |
// https://stackoverflow.com/questions/1779013/check-if-string-contains-only-digits |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let num = 8675309 | |
console.log(num.toString()) // --> "8675309" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
console.log(parseInt("8675309 hmu :)")) // --> 8675309 | |
console.log(parseFloat("3.14 is PI")) // --> 3.14 | |
// https://gomakethings.com/converting-strings-to-numbers-with-vanilla-javascript/ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function isAnagram (a, b) { | |
var y = a.split("").sort().join(""), | |
z = b.split("").sort().join(""); | |
return (z === y) | |
} | |
console.log(isAnagram("marek", "kemra")) // --> true | |
console.log(isAnagram("sautter", "sautrra")) // --> false | |
// https://stackoverflow.com/questions/23785465/javascript-anagram-comparison |