Ad
Code
Diff
  • function wordCount(str) {
      var words = 0;
      str.split(' ').forEach(function (w) {if (w.length) words++});
      return words;
    }
    • function wordCount(str) {
    • var words = 1;
    • for(var i = 1; i < str.length; i++) {
    • if(str[i] == " " && str[i - 1] !== " "&& str[i + 1] !== " ") {
    • words++;
    • }
    • }
    • var words = 0;
    • str.split(' ').forEach(function (w) {if (w.length) words++});
    • return words;