最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Simple JavaScript Word Count Function - Stack Overflow

programmeradmin6浏览0评论

I finally started to learn JavaScript and for some reason I can't get this simple function to work. Please tell me what I am doing wrong.

function countWords(str) {
/*Complete the function body below to count
  the number of words in str. Assume str has at
  least one word, e.g. it is not empty. Do so by
  counting the number of spaces and adding 1
  to the result*/

var count = 0;
for (int 0 = 1; i <= str.length; i++) {
   if (str.charAt(i) == " ") {
        count ++;
    }
}
return count + 1;
}
console.log(countWords("I am a short sentence"));

I am getting an error SyntaxError: missing ; after for-loop initializer

Thanks for your assistance

I finally started to learn JavaScript and for some reason I can't get this simple function to work. Please tell me what I am doing wrong.

function countWords(str) {
/*Complete the function body below to count
  the number of words in str. Assume str has at
  least one word, e.g. it is not empty. Do so by
  counting the number of spaces and adding 1
  to the result*/

var count = 0;
for (int 0 = 1; i <= str.length; i++) {
   if (str.charAt(i) == " ") {
        count ++;
    }
}
return count + 1;
}
console.log(countWords("I am a short sentence"));

I am getting an error SyntaxError: missing ; after for-loop initializer

Thanks for your assistance

Share Improve this question asked May 25, 2013 at 20:44 Val OkaforVal Okafor 3,44713 gold badges48 silver badges76 bronze badges 3
  • for loop construction should be: for (var i=0;i<=str.length;i++) – Nile Commented May 25, 2013 at 20:45
  • 1 @Nile: Actually i<str.length so that you don't loop outside the string. – Guffa Commented May 25, 2013 at 20:49
  • 3 Doing a loop through each char in the string will be highly inefficient I remend you do the following str.split(' ').length – elmuchacho Commented May 26, 2013 at 15:01
Add a ment  | 

4 Answers 4

Reset to default 5

There is no int keyword in Javascript, use var to declare a variable. Also, 0 can't be a variable, I'm sure that you mean to declare the variable i. Also, you should loop from 0 to length-1 for the characters in a string:

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

I think you want to write this

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

instead of this

for (int 0 = 1; i <= str.length; i++)

So the problems are there is nothing like int in javascript and also you are using 0=1 that doesn't make any sense. Just use variable i with var keyword.

This

for (int 0 = 1; i <= str.length; i++)

should be

for (var i = 1; i <= str.length; i++)

There is no keyword int in javascript

This is what you wanted:

function countWords(str) {
var count = 0,
i,
foo = str.length;

for (i = 0; i <= foo;i++;) {
if (str.charAt(i) == " ") {
count ++;
}

}
return console.log(count + 1);  
}


countWords("I am a short sentence");

btw. try to avoid declaring variables inside loop, it's faster when outside

发布评论

评论列表(0)

  1. 暂无评论