我终于开始学习 JavaScript,但由于某种原因,我无法让这个简单的函数工作。请告诉我我做错了什么。
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"));
我收到一个错误SyntaxError: missing ; after for-loop initializer
感谢你的协助