Javascript String countWords()
String.prototype.countWords = function(){ return this.split(/\s+/).length; } 'olly olly in come free'.countWords();
'use strict';/*w w w . j a v a 2s . co m*/ var words = function (input) { return input.countWords(); } String.prototype.countWords = function() { var splitString = this.split('\n').join(' ').split(' '); var obj = {}; for (var i = splitString.length - 1; i >= 0; i--) { if(!obj[splitString[i]]){ obj[splitString[i]] = 1; } else { ++obj[splitString[i]]; } }; return obj; }; module.exports = words;