Build a Sentence Analyzer - Step 7

Tell us what’s happening:

console.log() with an empty string or with characters is returning a result of 1. Should it be 1 or 0? It’s failing test #8.

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 word = sentence.toLowerCase().trim();
  let spaceInSentence = " ";
  let count = 0;
  for (let char of word) {
    if (spaceInSentence.includes(char)) {
      count++;
    }else if (word.length === 0 || word === "" || word === " " || word === "  ") {
      return count === 0;
    }
  } return count + 1; 
}

console.log(getWordCount(""))

// User Editable Region

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:147.0) Gecko/20100101 Firefox/147.0

Challenge Information:

Build a Sentence Analyzer - Step 7

it should be 0, there are no words in a string like " "