Build a Sentence Analyzer - Step 7

Tell us what’s happening:

Your word count should be case-insensitive? Not sure what this is referring to since my code meets the requirements.

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) {
  const space = " ";
  const punctuations = ".,!?;:-()[]{}\"'–";
  let count = 0;

  for (const char of sentence.toLowerCase()) {
    if (char.includes("'")){
      continue;
    }
    else if (space.includes(char) || punctuations.includes(char)) {
      count++;
  }
  }
  return count;
}

console.log(getWordCount("What's going on?"))

// 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/146.0.0.0 Safari/537.36 Edg/146.0.0.0

Challenge Information:

Build a Sentence Analyzer - Step 7

Hi @CboySmooth, and welcome to the FCC community!

Let’s re-evaluate the logic behind what a word is considered as in this step, which is any set of characters that is separated by spaces. Ignore any other characters at this point. What you’re focused on is spaces and ensuring that anything other than a space (or an empty string) is counted towards your total count of words.

2 Likes

YOU WERE RIGHT. Immediately after reading what you wrote I started wishing there was a way to just get rid of the spaces and worry about the rest later. Then BAM! the trim method popped back up

** removed by moderator **

Awesome, I’m glad to hear that! But please don’t post a solution directly here. It defeats the purpose because when you figure it out for yourself, you truly learn it!

If you want an extra challenge with this step, you can condense everything you wrote into a single return statement for this function and it will pass the test, as well. And it’s not even that long.

What I mean when I say that it can be one line is:

function getWordCount(sentence) {
  return codeHere;
}

If you do try this, you can send me a PM if you want if you get stuck, and I’ll show and explain the solution for a one liner.

1 Like

I cannot send you a PM since you’re a moderator, but here’s what I came up with and it passed!

** removed but well done! **

You can delete it, sorry This was the only way I could post it

1 Like