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 capitalize(str) { | |
let result = str[0].toUpperCase(); | |
for (let i = 1; i < str.length; i++) { | |
if (str[i - 1] === ' ') { | |
result += str[i].toUpperCase(); | |
} else { | |
result += str[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 capitalize(str) { | |
let result = ''; | |
for (let word of str.split(' ')) { | |
result += word[0].toUpperCase() + word.slice(1) + ' '; | |
} | |
return result.trim(); | |
} |
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 capitalize(str) { | |
const arr = str.split(' '); | |
return arr.map(word => { | |
return word[0].toUpperCase() + word.slice(1); | |
}).join(' '); | |
} |
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 anagrams(stringA, stringB) { | |
return cleanString(stringA) === cleanString(stringB); | |
} | |
function cleanString(str) { | |
return str.replace(/[^\w]/g, '').toLowerCase().split('').sort().join(''); | |
} |
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 anagrams(stringA, stringB) { | |
const aCharMap = charMap(stringA.replace(/[^\w]/g, '').toLowerCase()); | |
const bCharMap = charMap(stringB.replace(/[^\w]/g, '').toLowerCase()); | |
// return false immediately when the length of two char Map is not the same | |
if (Object.keys(aCharMap).length !== Object.keys(bCharMap).length) { | |
return false; | |
} | |
// 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
function chunk(array, size) { | |
if (!array) return []; | |
const firstChunk = array.slice(0, size); // create the first chunk of the given array | |
if (!firstChunk.length) { | |
return array; // this is the base case to terminal the recursive | |
} | |
return [firstChunk].concat(chunk(array.slice(size, array.length), size)); | |
} | |
/* |
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; | |
} |
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 = []; | |
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 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); | |
} |
NewerOlder