JavaScript function which checks the count of words in a string
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;