2

I am supposed to write a function which can count the words in the text area but my code count only the first word, you can see the code here:

link to my code

var myTextareaElement = document.getElementById("myWordsToCount");
myTextareaElement.onkeyup = function wordcount(wor){
  var myTextareaElement =document.getElementById("myWordsToCount").value;
  var haveByNow = 0;
  for (var i = 0; i < wordcount.length; i++)
    if (wor[i] === " ") {
      haveByNow = +1;
    }
  haveByNow += 1;
  document.getElementById("wordcount").innerHTML = haveByNow;
}
1

3 Answers 3

1

Why not splitting on empty character that textarea value and get arraylength:
Check the snippet below

var myTextareaElement = document.getElementById("myWordsToCount");
myTextareaElement.onkeyup = function wordcount(wor) {

  var myText = this.value.trim();
  var wordsArray = myText.split(/\s+/g);
  var words = wordsArray.length;


  document.getElementById("wordcount").innerHTML = words;
};
<textarea id="myWordsToCount"></textarea>

<span id="wordcount"></span>

Sign up to request clarification or add additional context in comments.

3 Comments

You should trim the value first; right now "ab ce " counts 3.
To followup on @Mathletics comment: you could also split on a whitespace regex: var wordsArray = myText.split(/\s+/g);
@Mathletics thanks for the heads up, don't know how I passed over that.
0

You have some typos and errors, see working code:

var myTextareaElement = document.getElementById("myWordsToCount");

myTextareaElement.onkeyup = function wordcount(wor){
    var words = myTextareaElement.value;
    var haveByNow = 1;
    for (var i = 0; i < words.length; i++) {
        if (words[i] == " ") {
            haveByNow += 1;
        }
    }
    document.getElementById("wordcount").innerHTML = haveByNow;
} 
<textarea id="myWordsToCount" rows="5" cols="60"></textarea><br>
The wordcount is: <span id="wordcount"></span><br>

Comments

0

This line:

haveByNow = +1;

...is not the same as:

haveByNow += 1;

The former sets haveByNow to 1, the other increments. This is why you'd be seeing a result of 1 every time.

Another problem is this expression in your for loop:

wordcount.length

wordcount is the name of your function, and wordcount.length is the number of parameters of that function, which is not going to be what you want. myTextareaElement, which is the value of the input is what you want to get the length of:

for (var i = 0; i < myTextareaElement.length; i++) ...

Though as @itsgoingdown points out, there are better ways to get a word count.

1 Comment

A function's .length property is the number of formal parameters the function declares, not the number of source code characters in its body.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.