//sample solution
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 (var i=0;i<str.length;i++) {
if (str.charAt(i) == " ") {
count ++;
}
}
return count + 1;
}
//test
countWords("We will prevail!");
No comments:
Post a Comment