Skip to content

Instantly share code, notes, and snippets.

@ilanl
Last active February 22, 2020 13:47
Show Gist options
  • Save ilanl/7c9d951332d0a4bc59ee5c3c69fc9361 to your computer and use it in GitHub Desktop.
Save ilanl/7c9d951332d0a4bc59ee5c3c69fc9361 to your computer and use it in GitHub Desktop.
Count Words
function reducer(acc, currentValue, currentIndex, arr) {
acc[currentValue] = (acc[currentValue]+1) || 1;
return acc;
}
function countWords(arrOfWords) {
return arrOfWords.reduce(reducer, {});
}
const mapCounts = countWords(["hello", "world", "hello", "dogs", "hello", "cats"], reducer, {});
console.log(mapCounts);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment