This word count does not work inside this blog!Please copy&past the code in the text area to
a text file and save it as a html file.
Here I've created a simple word count system with JavaScript.
I didn't expect that the code was so simple. What we need is a for-loop, RegExp object and a match() method. That's it.
Put the text in the text field and start counting its words by clicking the button "count". The text field is created with the textarea element of HTML. The "count" button has the "onclick" attribute and fires the "count()" function.
Now, let's look at the JavaScript code inside the count() function. We get the value inside the "textarea" with the following syntax:
document.getElementById("id of the element").value
We've got the data of the text typed inside the text area and next we want to count the words. So what's a word? To keep it simple, in this example a word is defined as a string containing one or more alphabets or numbers which is expressed by the metacharacter\w+. We want to look at the whole text therefore we use g for a global match. Here is the syntax:
id of the textarea element.match(/\w+/g)
Since we want to look at these word character chunks, we store the statement above in a variable. In the code example it's stored into "output". Now comes the for-loop to count each single chunk. The variable i stands for one chunk and to count them all we use the increment operator (i++). The counting should be continued regardless of the number of words therefore we need an infinite loop described by the statement i<(i+1). The brakets are necessary otherwise 1 is not added to the variable i.
Still, we have one problem. Metacharacters other than words are counted as "undefined". We just want to count the words so we have to get rid of "undefined". The break statement comes in handy. The syntax is as follows:
if (output==undefined){break;}
This statement removes all results marked as "undefined".
Here I've created a simple word count system with JavaScript.
I didn't expect that the code was so simple. What we need is a for-loop, RegExp object and a match() method. That's it.
Put the text in the text field and start counting its words by clicking the button "count". The text field is created with the textarea element of HTML. The "count" button has the "onclick" attribute and fires the "count()" function.
Now, let's look at the JavaScript code inside the count() function. We get the value inside the "textarea" with the following syntax:
document.getElementById("id of the element").value
We've got the data of the text typed inside the text area and next we want to count the words. So what's a word? To keep it simple, in this example a word is defined as a string containing one or more alphabets or numbers which is expressed by the metacharacter\w+. We want to look at the whole text therefore we use g for a global match. Here is the syntax:
id of the textarea element.match(/\w+/g)
Since we want to look at these word character chunks, we store the statement above in a variable. In the code example it's stored into "output". Now comes the for-loop to count each single chunk. The variable i stands for one chunk and to count them all we use the increment operator (i++). The counting should be continued regardless of the number of words therefore we need an infinite loop described by the statement i<(i+1). The brakets are necessary otherwise 1 is not added to the variable i.
Still, we have one problem. Metacharacters other than words are counted as "undefined". We just want to count the words so we have to get rid of "undefined". The break statement comes in handy. The syntax is as follows:
if (output==undefined){break;}
This statement removes all results marked as "undefined".
No comments:
Post a Comment