0

I'm trying to write a javascript counter function that takes a text argument that counts the number of words in the text.

The return output also has to be displayed as a single object.

brianchangdev
  • 27
  • 1
  • 7
  • 1
    What is a word? `str.match(/\w+/g).length;` – Paul S. Oct 18 '15 at 02:02
  • *The return output also has to be displayed as a single object.* You can display it however you want--as an alert, in the console, or by sticking it in the DOM. –  Oct 18 '15 at 04:55

2 Answers2

0

try to use text.split(' ').length

var countOf = function(text){
 return text.split(' ').length
} 
Rudolf Manusachi
  • 2,311
  • 2
  • 19
  • 25
0

the way to do is to count every is_space() in the string that is not preceded by an other is_space(), add to the result the last word if it remain.

milevyo
  • 2,165
  • 1
  • 13
  • 18