I have to develop code that reads through each option in an array while counting the vowels and consonants in each option. This also needs to be printed in the console.
let vowels = 0;
let consonants = 0;
const fruits = ["Apple", "Orange", "Pear", "Peach", "Strawberry", "Cherry", "Acai"];
for (let fruit in fruits) {
console.log(fruits[fruit]);
if (fruits[fruit] == "a" || fruits[fruit] == "e" || fruits[fruit] == "i" || fruits[fruit] == "o" || fruits[fruit] == "u") {
vowels + 1;
}
else {
consonants + 1;
}
}
This is what I have so far, can someone explain to me what I am missing?
Once I ran it in Visual Studio Code, the console still showed all the options in the array without registering if the variable’s Vowels or Consonants were even being addressed or increased:
Edit: I need to count each word in the array and display how many vowels and consonants are in each word.
[Running] node "d:coding.js"
Apple
Orange
Pear
Peach
Strawberry
Cherry
Acai
[Done] exited with code=0 in 0.128 seconds
2
Answers
or using regex, thanks to Dave’s comment below:
I’ve added some more console.logs to you code,
and I’ve used another variable
letter
.Do you want to look at just one letter per word,
or do you need to count every letter in every word?
You wrote that you need to count
every letter, so we need another loop to go through
each letter of the current word:
This still does not count uppercase letters. But I’m confident
you can fix this.