Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Function that loops through a string, creates a key for every letter and assigns letter occurrences in the string to the key value.
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