JavaScript function which checks the count of words in a string Task to accomplish: Made a function which counts the amount of words in a sentence.

Please don't put attention to the HTML. Is just for testing-purposes.



var sentence = document.querySelector('#sentence');
var exec = document.querySelector('#exec');
var result = document.querySelector('#result');

// ## THE ACTUAL FUNCTION ###########
// ## Rest is just for testing ######

// Reports the count of words which are 
//   contained in assigned string.

// Parameter (sentence): Are string is 
//   expected. The string doesn't have 
//   to be empty. It has to consist of
//   other characters then just blanks.

// Return a number: The count of words
//   in the assigned string.
//   In case of an error -1 is returned.

function getCountOfWords(sentence) {
  if (sentence.length === 0 || 
      sentence.search(/\w+/) === -1) 
    return -1;

  if (sentence.search(/\s+/) === -1)
    return 1;

  return (sentence.match(/(^|\s)\w+/g)).length;
}

// ## END ACTUAL FUNCTION ###########

function execHandler() {
  result.value = getCountOfWords(sentence.value);
}

exec.addEventListener('click', execHandler); 
.wrap {
  margin: 20px auto;
  width: 900px;
}

.wrap label {
  display: inline-block;
  width: 150px;
}

.wrap div {
  margin: 20px 0;
}
&lt;div class="wrap"&gt;
  &lt;div&gt;
    &lt;label for="sentence"&gt;
      Sentence to check:
    &lt;/label&gt;
    &lt;input type="text" id="sentence" 
           value="one Two2 Three 4four five123"
           size="80" /&gt;
  &lt;/div&gt;
  &lt;div&gt;
    &lt;button id="exec"&gt;Exec&lt;/button&gt;
  &lt;/div&gt;
  &lt;div&gt;
    &lt;label for="sentence"&gt;
      Count of words:
    &lt;/label&gt;
    &lt;input type="text" id="result" /&gt;
  &lt;/div&gt;
&lt;/div&gt;




I have tried to catch everything which could wrong.
But perhaps there are cases which I haven't thought about?

Are their better (more elegant) ways to achieve the described task?
This seems a bit like reinventing the wheel. I'm not sure on what you're trying to achieve here, but it doesn't look like you're accounting for hyphen separated words, so data-type counts as one word. 

It looks like to me that this is just splitting on the space, and it could be replaced by String.split(' ').length

I'm sorry if I missed the point here. But I'm gonna need you to explain it better why you need such a function in the first place.



var sentence = document.querySelector('#sentence');
var exec = document.querySelector('#exec');
var result = document.querySelector('#result');

function execHandler() {
  result.value = sentence.value.split(' ').length;
}

exec.addEventListener('click', execHandler); 
.wrap {
  margin: 20px auto;
  width: 900px;
}

.wrap label {
  display: inline-block;
  width: 150px;
}

.wrap div {
  margin: 20px 0;
}
&lt;div class="wrap"&gt;
  &lt;div&gt;
    &lt;label for="sentence"&gt;
      Sentence to check:
    &lt;/label&gt;
    &lt;input type="text" id="sentence" 
           value="one Two2 Three 4four five123"
           size="80" /&gt;
  &lt;/div&gt;
  &lt;div&gt;
    &lt;button id="exec"&gt;Exec&lt;/button&gt;
  &lt;/div&gt;
  &lt;div&gt;
    &lt;label for="sentence"&gt;
      Count of words:
    &lt;/label&gt;
    &lt;input type="text" id="result" /&gt;
  &lt;/div&gt;
&lt;/div&gt;