Skip to content

Instantly share code, notes, and snippets.

@roppa
Created January 18, 2016 11:50
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 roppa/8e502c4a31bdb8dfaf65 to your computer and use it in GitHub Desktop.
Save roppa/8e502c4a31bdb8dfaf65 to your computer and use it in GitHub Desktop.
Count letters in a string - returns an object with character for key and the character count for value
function countLetters (string) {
let letters = {};
for (let i = 0; i < string.length; i++) {
letters[string[i]] = letters[string[i]] + 1 || 1;
}
return letters;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment