Created
April 25, 2017 08:28
-
-
Save rishispeets/1438121a2cd47b8101998eee7407039c to your computer and use it in GitHub Desktop.
Function that loops through a string, creates a key for every letter and assigns letter occurrences in the string to the key 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 returnObject(aString){ | |
//object initializer | |
var obj = {}; | |
//loop through letters of string and populate keys | |
for (i = 0; i < aString.length; i++) { | |
obj[aString[i]] = (obj[aString[i]] || 0) + 1; | |
} | |
//return obj to function | |
return obj; | |
} | |
console.log(returnObject("helloooo")); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment