skip to Main Content

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


  1. const fruits = ["Apple", "Orange", "Pear", "Peach", "Strawberry", "Cherry", "Acai"];
    
    const vowels = fruits.flatMap(i=>[...i.toLowerCase()])
      .filter(c=>'aeiou'.includes(c)).length;
    
    const consonants = fruits.join('').length - vowels;
    
    console.log(vowels, consonants)

    or using regex, thanks to Dave’s comment below:

    const fruitsString = ["Apple", "Orange", "Pear", "Peach", "Strawberry", 
      "Cherry", "Acai"].join('')
    
    const vowels = fruitsString.match(/[aeiou]/ig).length
    
    const consonants = fruitsString.length - vowels
    
    console.log(vowels, consonants)
    Login or Signup to reply.
  2. 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?

        let vowels = 0;
        let consonants = 0;
        const fruits = ["Apple", "Orange", "Pear", "Peach", "Strawberry", "Cherry", "Acai"];
        
        for (let word of fruits) {
            console.log("working on word", word);
        
            letter = word[0];   // this is probably wrong, do you need to check EVERY letter of a word?
        
            console.log("  looking at letter", letter);
        
            if (letter == "a" || letter == "e" || letter == "i" || letter == "o" || letter == "u") {
                vowels + 1;
            }
            else {
                consonants + 1;
            }
            console.log("  up to now i found", vowels, "vowels and", consonants, "consonants");
        }
    

    You wrote that you need to count
    every letter, so we need another loop to go through
    each letter of the current word:

            let vowels = 0;
            let consonants = 0;
            const fruits = ["Apple", "Orange", "Pear", "Peach", "Strawberry", "Cherry", "Acai"];
            
            for (let word of fruits) {
                console.log("working on word", word);
                
                for (let letter of word.split('')) {
    
                  console.log("  looking at letter", letter);
    
                  if (letter == "a" || letter == "e" || letter == "i" || letter == "o" || letter == "u") {
                      vowels += 1;
                  }
                  else {
                      consonants += 1;
                  }
                  console.log("  up to now i found", vowels, "vowels and", consonants, "consonants");
                
                }
            }

    This still does not count uppercase letters. But I’m confident
    you can fix this.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search