Quotes help make search much faster. Example: "Practice Makes Perfect"

Friday, December 7, 2012

LearnStreet Javascript Lesson 6 Exercise 6


//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


This is an example of scrolling text using Javascript.

Popular Posts