Skip to content

Instantly share code, notes, and snippets.

@mwakipesile
Last active May 4, 2018 00:24
Show Gist options
  • Save mwakipesile/02f6322d3cb3608dc9ba840481b5bf2c to your computer and use it in GitHub Desktop.
Save mwakipesile/02f6322d3cb3608dc9ba840481b5bf2c to your computer and use it in GitHub Desktop.
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