Build a Sentence Analyzer - Step 7

Tell us what’s happening:

My function below seems to fail the test cases for step 7. Here are the console output:

3. Your getWordCount function should return a number.
4. When the sentence is "When are you gonna start learning to code?", the getWordCount function should return 8.
5. When the sentence is "What's going on?", the getWordCount function should return 3.
6. Your word count should be case-insensitive.
7. Your getWordCount function should return the correct word count for any sentence.

I made my own test cases and it passed.

Your code so far

function getVowelCount(sentence) {
  const vowels = "aeiou";
  let count = 0;

  for (const char of sentence.toLowerCase()) {
    if (vowels.includes(char)) {
      count++;
    }
  }
  return count;
}

const vowelCount = getVowelCount("Apples are tasty fruits");
console.log(`Vowel Count: ${vowelCount}`);

function getConsonantCount(sentence) {
  const consonants = "bcdfghjklmnpqrstvwxyz";
  let count = 0;

  for (const char of sentence.toLowerCase()) {
    if (consonants.includes(char)) {
      count++;
    }
  }
  return count;
}

const consonantCount = getConsonantCount("Coding is fun");
console.log(`Consonant Count: ${consonantCount}`);

function getPunctuationCount(sentence) {
  const punctuations = ".,!?;:-()[]{}\"'–";
  let count = 0;

  for (const char of sentence) {
    if (punctuations.includes(char)) {
      count++;
    }
  }
  return count;
}

const punctuationCount = getPunctuationCount("WHAT?!?!?!?!?");
console.log(`Punctuation Count: ${punctuationCount}`);


// User Editable Region

function getWordCount(sentence){
  let count = sentence.trim().length > 0 ? 1 : 0
  let isPreviousSpace = false
  for (char of sentence.trim()){
    if(char === " "){
      if (isPreviousSpace) {
        continue;
      } else {
        count++;
      }
      isPreviousSpace = true
    } else {
      isPreviousSpace = false
    }
  }
  return count
}

// User Editable Region

Your browser information:

User Agent is: Mozilla/5.0 (X11; Linux x86_64; rv:146.0) Gecko/20100101 Firefox/146.0

Challenge Information:

Build a Sentence Analyzer - Step 7

I am getting an error when I test it. You don’t see an error in the console?

If I log a call to getWordCount(), I get a syntax error in the console.

ReferenceError: char is not defined

No. This works on programiz with no error:

function getWordCount(sentence){
let count = sentence.trim().length > 0 ? 1 : 0
let isPreviousSpace = false
for (char of sentence.trim()){
if(char === " "){
if (isPreviousSpace) {
continue;
} else {
count++;
}
isPreviousSpace = true
} else {
isPreviousSpace = false
}
}
return count
}

console.log(getWordCount(“Hello World!”))
console.log(getWordCount(" “))
console.log(getWordCount(” "))

No. There is no error showing when i tried it on programiz tho. Here is the code I tested:

function getWordCount(sentence){
let count = sentence.trim().length > 0 ? 1 : 0
let isPreviousSpace = false
for (char of sentence.trim()){
if(char === " "){
if (isPreviousSpace) {
continue;
} else {
count++;
}
isPreviousSpace = true
} else {
isPreviousSpace = false
}
}
return count
}

console.log(getWordCount(“Hello World!”))
console.log(getWordCount(" “))
console.log(getWordCount(” "))

Ohh I figured it out. It turns out, programiz (online compiler) is allowing char of sentence.trim() and not producing error.

The fCC platform uses JavaScript “strict mode” which will behave a bit differently.

You need to use let or const.

1 Like

When I executed the script, I saw an error in the console.