Build a Sentence Analyzer - Step 7

Tell us what’s happening:

How do I detect a string filled with spaces to put into my first if-statement. ’ ’ slips into the else statement.

Your code so far

// User Editable Region

function getWordCount(sentence){
  let count=1;
  if (sentence.length===0){
    return 0
  } else {for (const char of sentence) {
    if (char===' '){
      count++;
    }
  }
  return count
  }
}

console.log(getWordCount('   '))

// User Editable Region

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/143.0.0.0 Safari/537.36

Challenge Information:

Build a Sentence Analyzer - Step 7

It would be easier to count the words, rather than extrapolating from counting the spaces between them. How could you (very easily) break up a sentence into its constituent words, given that all words in a sentence are separated by a single space?

1 Like