0
 function countTheWords() {
    var word = "";
    var num;
    num = num + 1;

How do i add a counter to this? I'm trying to count how many words there are in a sentence. I have a test next week that I am studying for and I am stuck on this. I know that what I have is wrong, so please guide me on how I can count the number of words in a sentence.

    do {
    word = prompt("Enter a sentence one word at a time. Enter exit to finish your sentence.");
    word = word + 1;
    }while(word != "exit"); 


    document.write("There are: " + word + " words in your sentence");
}
Nicole
  • 5
  • 1
  • 6

5 Answers5

0

To test how many words are in a sentence you could do something like:

 function wordCount(str){
   return str.match(/\s/g).length+1;
 }

In real practice you won't use prompt or alert, but this might help too:

 var sen = prompt('Please enter a sentence.');
 alert('There are '+wordCount(sen)+' words in your sentence!');
StackSlave
  • 10,613
  • 2
  • 18
  • 35
0

you can split based on word separator for example " " and new lines.

an example:

var str = "sample sentence"; // or var str=prompt("Enter a sentence")
var x = str.split(" ");      // use newline or any separator if reqd
document.write(x.length)
RobG
  • 142,382
  • 31
  • 172
  • 209
0

One way to solve this problem is by using Regular Expressions. For every word entered, check for the word exit in it just by doing:

/exit/.test(word);

This will return a boolean value that you can test.

Here is the updated code:

var wordCount = 0;

do {
  word = prompt("Enter a sentence one word at a time. Enter exit to finish your sentence.");
  wordCount++;
}while(/exit/.test(word) === false); 


document.write("There are: " + wordCount + " words in your sentence");

Read up: Regular Expressions - JavaScript | MDN

You can make use of arrays to count the words:

var sentence = "This is a sentence.";

var wordCount = sentence.split(' ').length;

console.log(wordCount);  // check your browser's Javascript console to see the count
Rahul Desai
  • 15,242
  • 19
  • 83
  • 138
  • The *test* method returns true or false, so `while (!/exit/.test(word))` seems more appropriate, but `while (word != 'exit')` is a lot simpler and is an exact match, whereas the former tests for "exit" anywhere in *word*. A *while* condition is not terminated by a semicolon. In the above, it represents an empty statement. – RobG Nov 13 '15 at 03:18
  • @RobG I just wanted to be more specific to the newbie OP :) by saying `/exit/.test(word) === false`. Since the `.prompt()` is asking for a sentence, i believe it would be more appropriate to check for `exit` in the whole sentence. – Rahul Desai Nov 13 '15 at 03:21
  • @RobG MDN has an example where `do .. while` ends with `;` https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/do...while – Rahul Desai Nov 13 '15 at 03:22
  • Hmm… MDN isn't an authority, but [*ECMA-262*](http://www.ecma-international.org/ecma-262/6.0/index.html#sec-do-while-statement) seems to agree. But I can't find a case where it's needed, let me think about it…now I want to know where it's required. – RobG Nov 13 '15 at 07:04
  • I can't find a case where the semicolon is necessary to prevent ambiguity. So while strictly it's required, in practice it seems redundant. – RobG Nov 14 '15 at 11:30
0

You can use something like that:

function wordCounter(word){
   return word.split(' ').length
}
Joel R Michaliszen
  • 4,164
  • 1
  • 21
  • 28
0

Have you tried:

function countWords(sentence) {
    //Creates an array of all of the words and gets the array's length
    return sentence.split(" ").length;
}

alert(countWords("A very simple function can save lots of time")); // 9
Andrew Mast
  • 311
  • 1
  • 17