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
return Object.keys(obj).reduce((prev, next) => obj[a] >= obj[b] ? a : b); |
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
Object.keys(obj).forEach(char => { | |
if (obj[char] > max) { | |
maxChar = char; | |
max = obj[char]; | |
} | |
}); |
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
const obj = {}; | |
str.split('').forEach(char => obj[char] + 1 || 1); // remember to turn the given string into array first as forEach is Array prototype |
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
const obj = {}; | |
for (let char of str) { | |
obj[char] = obj[char] + 1 || 1; | |
} |
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 maxChar(str) { | |
const charMap = {}; | |
let max = 0; | |
let maxChar = ''; | |
// create character map | |
for (let char of str) { | |
if (charMap[char]) { | |
// increment the character's value if the character existed in the map | |
charMap[char]++; |
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 reverseInt(n) { | |
const reversed = n.toString().split('').reverse().join(''); // turn a number into a string, then turn it into an array to reverse. | |
return Math.sign(n) * parseInt(reversed); // Math.sign will return -1 as for negative number, 1 as for position number, 0 as for zero. | |
} | |
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 palindrome(str) { | |
for (var i = 0; i < str.length / 2; i++) { | |
if (str[i] !== str[str.length - 1 -i]) { | |
return false; | |
} | |
} | |
return true; | |
} |
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 palindrome(str) { | |
const arr = str.split(''); // turn a string in to an array of characters | |
return arr.every((char, i) => char === str[str.length - 1 - i]); // return true if all elements in the array pass the test implemented by the provided function. | |
} |
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 palindrome(str) { | |
const reversed = str.split('').reverse().join(''); | |
return str === reversed; | |
} |
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 reverse(str) { | |
let reversed = ''; | |
for (var i = str.length - 1; i >= 0; i--) { | |
reversed += str[i]; | |
} | |
return reversed; | |
} |