Last active
May 4, 2018 00:24
-
-
Save mwakipesile/02f6322d3cb3608dc9ba840481b5bf2c to your computer and use it in GitHub Desktop.
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
function wordCount(str) { | |
if (typeof str !== 'string' || !/\w+/.test(str)) return 'Error: bad input'; | |
var words = str.toLowerCase().match(/(\w+(?<!'\w+)'\w+|\w+)/g); | |
var incrementWordCount = (word_count, word) => { | |
word_count[word] = 1 + (word_count[word] | 0) | |
return word_count; | |
}; | |
return words.reduce(incrementWordCount, {}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment