Created
January 18, 2016 11:50
-
-
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
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
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