The JavaScript file

This is the JavaScript file:

In this example, as long as i will be smaller than array length, this code will run.

var myTextareaElement = document.getElementById("myWordsToCount");
myTextareaElement.onkeyup = function(){
  var i=0;
  var sum=0;
  var myValue = document.getElementById("myWordsToCount").value;
  var res = myValue.split(" ");
  while (i<res.length) {
    sum=sum+1;
    i=i+1;
  }
  document.getElementById("wordcount").innerHTML = sum;
  console.log(res, i, res.length);
};

Exercise

  1. Open your editor, create a new file and save it as countsTheWords.js in the folder "yourNameWEB2JS/js".
  2. Copy the above code and paste it into the new file.
  3. Modify and save the file. Preview the exersice0873.01.html file. Is there any difference from the introductory video?

Solution:

Yes, the wordcount increases even extra 'whitespace' characters are inserted in your text