Last active
August 29, 2015 14:19
-
-
Save shamod/4362a6eec0b092769b3c to your computer and use it in GitHub Desktop.
WordCount - Functional
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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