Tell us what’s happening:
My function below seems to fail the test cases for step 7. Here are the console output:
3. Your getWordCount function should return a number.
4. When the sentence is "When are you gonna start learning to code?", the getWordCount function should return 8.
5. When the sentence is "What's going on?", the getWordCount function should return 3.
6. Your word count should be case-insensitive.
7. Your getWordCount function should return the correct word count for any sentence.
I made my own test cases and it passed.
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 = sentence.trim().length > 0 ? 1 : 0
let isPreviousSpace = false
for (char of sentence.trim()){
if(char === " "){
if (isPreviousSpace) {
continue;
} else {
count++;
}
isPreviousSpace = true
} else {
isPreviousSpace = false
}
}
return count
}
// User Editable Region
Your browser information:
User Agent is: Mozilla/5.0 (X11; Linux x86_64; rv:146.0) Gecko/20100101 Firefox/146.0
Challenge Information:
Build a Sentence Analyzer - Step 7