Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
WordCount - Functional
var paragraph = 'How much wood would a woodchuck chuck\nIf a woodchuck could chuck wood?\nHe would chuck, he would, as much as he could,\nAnd chuck as much as a woodchuck would\nIf a woodchuck could chuck wood.';
var paraCount = paragraph.split('\n')
.reduce(joinArray)
.replace(/[^\w\s]/g, '')
.split(/\s+/)
.reduce(wordCount, {});
function joinArray(last, now) {
return last.toLowerCase().concat(' ' + now.toLowerCase());
}
function wordCount(map, word) {
map[word] = (map[word] || 0) + 1;
return map;
}
console.log(paraCount);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.