Book
Submodule 7.3: Word count
Submodule 7.3: Word count
Completion requirements
View
- JavaScript word counter
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
- Open your editor, create a new file and save it as
countsTheWords.js
in the folder "yourNameWEB2JS/js". - Copy the above code and paste it into the new file.
- Modify and save the file. Preview the
exersice0873.01.html
file. Is there any difference from the introductory video?
Yes, the wordcount increases even extra 'whitespace' characters are inserted in your text