Saturday, 7 September 2013

Counting words in string

Counting words in string

function WordCount(str) {
var totalSoFar = 0;
for (var i = 0; i < WordCount.length; i++)
if (str(i) === " ") { // if a space is found in str
totalSoFar = +1; //add 1 to total so far
}
totalsoFar += 1; //adds 1 to totalsoFar to account for extra space
since 1 space= 2 words
}
console.log(WordCount("Random String")
I think I have got this down pretty well,except I think that the if
statement is wrong how do I say if(str(i) Contains a space add 1. Thanks
in advance

No comments:

Post a Comment