Skip to content

Instantly share code, notes, and snippets.

@IPRIT
Created April 11, 2015 14:57
Show Gist options
  • Select an option

  • Save IPRIT/92d566c1e5772a8ae15f to your computer and use it in GitHub Desktop.

Select an option

Save IPRIT/92d566c1e5772a8ae15f to your computer and use it in GitHub Desktop.
var util = require('util');
function countWords(inputWords) {
return inputWords.reduce(function (prev, cur) {
if (!util.isObject(prev)) {
var obj = {};
obj[prev] = 1;
obj[cur] = 1;
return obj;
}
if (!prev[cur]) {
prev[cur] = 0;
}
++prev[cur];
return prev;
});
}
module.exports = countWords;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment