Skip to content

Instantly share code, notes, and snippets.

@sandrabosk
Last active November 2, 2019 17:52
Show Gist options
  • Save sandrabosk/72433e80d08721fe205101c5884dcc4c to your computer and use it in GitHub Desktop.
Save sandrabosk/72433e80d08721fe205101c5884dcc4c to your computer and use it in GitHub Desktop.
const someString = "hey there hey what up what"
const howMany = (blah) => {
let arrOfWords = blah.split(" ");
const mappedObj = {};
for(let i=0; i<arrOfWords.length; i++){
if(mappedObj[arrOfWords[i]]){
mappedObj[arrOfWords[i]]++
} else {
mappedObj[arrOfWords[i]]=1
}
}
return mappedObj;
}
howMany(someString);
// {
// hey: 2,
// there: 1,
// what: 2,
// up: 1
// }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment