Skip to content

Instantly share code, notes, and snippets.

@rishispeets
Created April 25, 2017 08:28
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 rishispeets/1438121a2cd47b8101998eee7407039c to your computer and use it in GitHub Desktop.
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.
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