Javascript String Question 9

Word Count

Create a function that returns the number of words in a string.

Words will be separated by single spaces.

function wordCount(str) {
    //your code here
}

// Output
console.log(wordCount("this is a test"));//3
console.log(wordCount("abc"));//1



function wordCount(str) {
    return str = str.split(" ").length;
}

// Output
console.log(wordCount("this is a test"));
console.log(wordCount("abc"));



PreviousNext

Related