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 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
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
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
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
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
function maxChar(str) { | |
const obj = {}; | |
str.split('').forEach(char => obj[char] + 1 || 1); | |
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
function chunk(array, size) { | |
const chunked_arr = []; | |
for (let i = 0; i < array.length; i++) { | |
const last = chunked_arr[chunked_arr.length - 1]; | |
if (!last || last.length === size) { | |
chunked_arr.push([array[i]]); | |
} else { | |
last.push(array[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 chunk(array, size) { | |
const chunked_arr = []; | |
let copied = [...array]; // ES6 destructuring | |
const numOfChild = Math.ceil(copied.length / size); // Round up to the nearest integer | |
for (let i = 0; i < numOfChild; i++) { | |
chunked_arr.push(copied.splice(0, size)); | |
} | |
return chunked_arr; | |
} |
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 chunk(array, size) { | |
const chunked_arr = []; | |
let index = 0; | |
while (index < array.length) { | |
chunked_arr.push(array.slice(index, size + index)); | |
index += size; | |
} | |
return chunked_arr; | |
} |