Build a Sentence Analyzer - Step 7

Tell us what’s happening:

no.8 fails, what should be fixed here guys?

8. Your getWordCount function should return the correct word count for an empty string, or a string only with spaces.

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 check = sentence.toLowerCase().split(" ");
  let number = 0;
  let unused = 0;
  for (let checkThrough of check) {
    number++;
    console.log(checkThrough)
    
  }
  for (let emptyCheck of check) {
     if(emptyCheck.includes('')) {
      unused++;
    }
  }
  console.log(unused);
  return number;
}
console.log(getWordCount("I see the storm coming"));

// 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/139.0.0.0 Safari/537.36 OPR/123.0.0.0

Challenge Information:

Build a Sentence Analyzer - Step 7

[Spoilers]

Your logic is good, but the error message “8. Your getWordCount function should return the correct word count for an empty string, or a string only with spaces.” shows that you forgot to check for white spaces or an empty string in your logic.
Every time, you return a number the number of spaces even if the string only has spaces. To fix this, you can trim the sentence, check if it’s empty, and finally get the correct number. I refactored your code

code removed by moderator

Good Job and Happy Code :ok_hand::+1:

hi @LaudeDignus

It is great that you solved the challenge, but instead of posting your full working solution, it is best to stay focused on answering the original poster’s question(s) and help guide them with hints and suggestions to solve their own issues with the challenge. How to Help Someone with Their Code Using the Socratic Method

We are trying to cut back on the number of spoiler solutions found on the forum and instead focus on helping other campers with their questions and definitely not posting full working solutions.

hi @ricot

have you tried to test what your function does with an empty string and strings containing only spaces?

these would be

console.log(getWordCount(""));
console.log(getWordCount(" "));
console.log(getWordCount("  "));
console.log(getWordCount("                               "));

it increment every time i add space

it worked i finally saw the problem and fix it