Last active
January 3, 2018 08:24
-
-
Save eldyvoon/49af57ec3b3b604cecaab0f95e625ae9 to your computer and use it in GitHub Desktop.
Characters Map in JavaScript
This file contains 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
//specs | |
const str = 'nice to meet you!'; | |
buildCharMap(str); // {"n":1,"i":1,"c":1,"e":3," ":3,"t":2,"o":2,"m":1,"y":1,"u":1,"!":1} | |
function buildCharMap(str) { | |
const char = {}; | |
for(let character of str) { | |
char[character] = char[character] && char[character] + 1 || 1; | |
} | |
return char; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment