Build a Sentence Analyzer - Step 7

Tell us what’s happening:

Getting desired result but test not passing, please help me fix this

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 = 0;
  for(let char of sentence) {
    if(char === " ") {
      count++;
    }
  } return count + 1;
}

console.log(getWordCount("When are you gonna start learning to code?"));

// 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/131.0.0.0 Safari/537.36

Challenge Information:

Build a Sentence Analyzer - Step 7

1 Like
function getWordCount(sentence) {
  let count = 0;
  for(let char of sentence) {
    if(char === " ") {
      count++;
    }
  } return count + 1;
}

console.log(getWordCount("When are you gonna start learning to code?"));

The failing test is

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

Have you tried your function with arguments " " or "" or " "?

there is a problem, with my logic because when there is empty string then it adds 1 and for string with spaces it counts no. of spaces. I need to change my approach. Please guide me in right direction little bit.

I simply thought there is only 1 more word then number of spaces so I did this

you will need to consider a different approach, it seems that counting spaces doens’t work
even if you don’t know how to code it, if you can describe the logic, we can start from somewhere

I can extract each word of a sentence which are separated by space.

I did some google and then I revised the string method split, then I wrote like this-

function getWordCount(sentence) {
  let count = 0;
  let abc = sentence.split(" ");
  for(let char of abc) {
    if(char === "") {
      return 0;
    } else if(char !== "") {
      count++
    }
  } return count;
}

console.log(getWordCount("When are you gonna start learning to code?"));
console.log(getWordCount(""));
console.log(getWordCount("  "));

Is this correct ?

Hi there. Your last updated code is passing the test.

Yes but the solution provided after passing test is very different, I don’t even understand that.

This code was provided-

function getWordCount(sentence) {
  if (sentence.trim() === '') {
    return 0;
  }
  
  const words = sentence.trim().split(/\s+/);
  return words.length;
}```

And i don't understand this part at all-

split(/\s+/)

The trim() method removes leading and trailing spaces from the input string.
If the string is something like " " or " word ", trim() ensures we are working only with the actual content, excluding spaces at the start or end.
After trimming, the string is split into words using .split() and a regular expression (/\s+/).
/\s+/ is a regular expression that matches one or more whitespace characters (spaces, tabs, line breaks).
It ensures that even multiple consecutive spaces between words are treated as a single separator.

After splitting the string into words, the function returns the length of the resulting array, which corresponds to the total number of words in the string.

2 Likes

Got it. I know split method but I have not studied that expression thing. I think it will be taught later. Thank you @hasanzaib1389 :blush:

4 Likes