I know this is a basic questions, but I'm trying to work through a beginner's coding problem and I'm stuck on why this code doesn't work.
This is what I have so far:
const myString = "Testing for vowels";
const vowels = ["a", "e", "i", "o", "u", "y"];
function countVowels(myString) {
let count = 0;
if (vowels.includes(myString)) {
count++;
}
return count
}
const result = countVowels(myString);
console.log(result);
When it logs out, it just stays 0
. Clearly I'm missing something, but I'm not sure what it is at this point. I've set up what vowels are, I think I'm asking that, if the vowels I set in the array are in my string, increase the count, but it's just giving me 0
. I know it must be obvious, but I'm stuck.
I've tried a for
loop, then I thought it may need to be an if
statement, now I have that in a function. I haven't been able to get the answer either way, so I don't think I'm properly telling the code what the vowels are, but I'm not sure how to do that.
you have to use a for loop in order to check if each character is included in the vowels array like this :
const myString = "Testing for vowels";
const vowels = ["a", "e", "i", "o", "u", "y"];
function countVowels(myString) {
let count = 0;
for(let i = 0 ; i < myString.length ; i++) {
if(vowels.includes(myString[i])) {
count++ ;
}
}
return count
}
const result = countVowels(myString);
console.log(result);