My code return the correct count of vowels but not passed.
The error has been raised: Your getVowelCount function should return the correct vowel count for any sentence.
Your code so far
// User Editable Region
function getVowelCount(sentence){
//the vowels chars
const vowels=['a','e','i','u','o'];
//check the input
if(sentence === null || sentence === '')
return -1;
//prepare counter and change sentence to lowercase
let count=0;
let toLower=sentence.toLowerCase();
//search and count
for(let char of toLower){
if(vowels.includes(char))
count++;
}
//return the count
return count;
}
// 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/132.0.0.0 Safari/537.36
Hey there,
what’s the purpose of if(sentence === null || sentence === '') return -1;?
Is the if statement correct there?
Does the step ask you to do this?
Hi, thank you for answering my question. I removed that and passed.
but in programming, it’s a typical way to prevent errors. what’s the problem with that?
For one, you do not use exit status codes in JavaScript. You would throw or return something more useful than a number.
It might be used in C/C++ to return an integer like 0, 1, or -1 (or whatever) in procedural functions that do side effects to indicate if it was successful or not. But if a function is expected to return a count, and 0 is a valid count to return from an empty string, then it doesn’t make sense to return -1. The function isn’t unsuccessful when it gets an empty string, it is expected to return the count, which would be 0.