Build a Sentence Analyzer - Step 7

Tell us what’s happening:

Why do they expect me to know regex when it hasn’t been taught to me :frowning: ?

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 lwerTrimSentence = sentence.toLowerCase().trim();
  let spaceInSentence = " ";
  let count = 0;
  for (let char of lwerTrimSentence){
    if(spaceInSentence.includes(char)){count++
    count + 1;}
    else if(lwerTrimSentence.length=== 0 && lwerTrimSentence === ""){return count = 0 };
    
  }
  return count + 1
}
console.log(getWordCount("When are you gonna start learning to code?"));
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

You don’t need regex to pass the test.

When I ran your code the error message was:

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

The issue is your edge case here:

else if(lwerTrimSentence.length=== 0 && lwerTrimSentence === ""){return count = 0 };
    
  }

You have not accounted for the possibility of a string with only multiple spaces, so

"        "

for example. You need to adjust that part of the code. Think how you could use the existing functions to assist with that.

Preciate you so I need to account for as many possibilities as possible

just the 2 that they say - an empty string or a string with one or more spaces.

I would probably put those edge cases first to get them out of the way.