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 getAllPermutations(string) { | |
var results = []; | |
if (string.length === 1) { | |
results.push(string); | |
return results; | |
} | |
for (var i = 0; i < string.length; i++) { | |
var firstChar = string[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
function reverseWords(str) { | |
var result = ""; | |
var wordArray = str.split(" "); | |
for(var i = wordArray.length - 1; i >= 0; i--) { | |
result += wordArray[i] + " "; | |
} | |
return result.trim(); | |
} | |
console.log(reverseWords("Marek E. Sautter")); // --> "Sautter E. Marek" |
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 firstNonRepeat(string) { | |
return string.split('').filter(function (character, index, obj) { | |
return obj.indexOf(character) === obj.lastIndexOf(character); | |
}).shift(); | |
} | |
console.log(firstNonRepeat("----Marek-----")); // --> "M" |
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 checkPalindrome (str) { | |
return str == str.split('').reverse().join(''); | |
} | |
console.log(checkPalindrome("Marek")); // --> false | |
console.log(checkPalindrome("kayak")); // --> true | |
// https://stackoverflow.com/questions/14813369/palindrome-check-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 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 |
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
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
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
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
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 |