Skip to content

Instantly share code, notes, and snippets.

@eldyvoon
Last active January 3, 2018 08:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save eldyvoon/49af57ec3b3b604cecaab0f95e625ae9 to your computer and use it in GitHub Desktop.
Save eldyvoon/49af57ec3b3b604cecaab0f95e625ae9 to your computer and use it in GitHub Desktop.
Characters Map in JavaScript
//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