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<str.lengthso that you don't loop outside the string.