Tell us what’s happening:
I am getiing one error which is
expected 9 to equal 7
I cannot figure out where I am missing something. I logged everything but still nothing. as the length of every passed in value is more than 10.
Can I get a hint?
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}`);
// User Editable Region
function getConsonantCount(sentence){
const removeSpaces = sentence.toLowerCase().split(' ');
const join = removeSpaces.join("");
const consonant = [];
console.log(join, join.length)
const vowels = ['a','e','i','o','u'];
for(let i = 0; i <=join.length;i++){
if(join[i] === vowels[0] ||
join[i] === vowels[1] ||
join[i] === vowels[2] ||
join[i] === vowels[3] ||
join[i] === vowels[4]
){
continue
}else{
consonant.push(join[i])
}
}
return consonant.length - 1;
}
// User Editable Region
Your browser information:
User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/142.0.0.0 Safari/537.36
Challenge Information:
Build a Sentence Analyzer - Step 3
There are simpler ways to go about discounting vowels, but the issue is that you appear to be counting non-alphabet characters as consonants.
For example, the string “Hello World!” should return 7 but your function is also counting the ! and returning 8.
EDIT: You also have an issue with your loop and with the value you’re returning. Try logging consonant just before the return statement to see what the array contains.
HINT: A simpler approach might use a for … of loop and the includes method (or a regex match), for instance.
1 Like
I see you mention regex too but I am not to that level yet, so taking small steps. Also I am looking for only consonants and not vowel which also might include punctuation like !, I can put a another clause for ! as well?
Do you mean the length? as last item is undefined? If so i think it is becaus when the increment happens and then I am logging the value. Either I can do length-1 in the loop or like I did in later on?
When you loop over an array or a string, which have zero-based indexing, the index of the first item is 0 (not 1), so the index at the end of the array/string will not be the same as its length.
EXAMPLE:
let myString = "hello"
console.log(myString.length) // 5
console.log(myString[0]) // h
console.log(myString[4]) // o
console.log(myString[5]) // undefined
So, you should always terminate the loop at length-1 in these cases.
Your function should return the number of consonants in a string, so rather than eliminating vowels, spaces and then all other non-alphabet characters, it might be simpler to look directly for consonants. For instance, you could compare each character in the input string against a string containing all of the consonants. You don’t need a long chain of conditional statements to do this. As I said above, check out the includes method, which works on both arrays and strings.
HINT: You can create arrays if you like but it’s not necessary, as you could iterate the input string directly. To count consonants, you could use a variable which you increment every time you encounter a consonant.
So this is my solution
function getConsonantCount(sentence){
const removedSpaces = sentence.toLowerCase();
const listOfConsonant = [‘b’,‘c’,‘d’,‘f’,‘g’,‘h’,‘j’,‘k’,‘l’,‘m’,‘n’,‘p’,‘q’,‘r’,‘s’,‘t’,‘v’,‘w’,‘x’,‘y’,‘z’];
let consonants = ;
for(let i = 0; i <= removedSpaces.length - 1; i++){
for(let x = 0; x <=listOfConsonant.length - 1;x++){
console.log({
rmspaces: removedSpaces\[i\],
listOfConsonant: listOfConsonant\[x\]
})
if(removedSpaces[i] === listOfConsonant){
consonants.push(listOfConsonant\[i\])
}
}
}
return consonants.length;
It passed but I have one question which is do I need to remove the empty space between each word before I compare, although the spaces were not a match but maybe it is a cleaner way?
P.S. - I still have to try with your includes() method.