Code Check

Hello, I’m studying JS and there’s a mini-activity I needed to do. It’s using .forEach(). Now I’ve gotten the activity however, it printed

I want to eat a mango
I want to eat a apple

I just want to add a condition that would change the articles (a /an) so I made this code. The first line is printing correctly but the succeeding lines are not. I would greatly appreciate it if someone could point me to where the errors are in my code. Thank you!

const fruits = [“Mango”, “papaya”, “pineapple”, “apple”];

// Iterate over fruits below
eatFruits = (fruits) => {
const vowels = [‘a’,‘e’,‘i’,‘o’, ‘u’]
const firstLetter = fruits.charAt(0);
for (let i = 0; i<vowels.length; i++)
if (firstLetter === vowels[i]) {
console.log('I want to eat an '+ fruits)
} else {
console.log('I want to eat a '+ fruits)
// prints
I want to eat an apple
I want to eat a apple
I want to eat a apple
I want to eat a apple
I want to eat a apple

There are several errors in your code.
The most important being:

forEach is a method (function). It needs to be followed by parenthesis:


Your eatFruits callback needs to accept a parameter for a single item (fruit):

const eatFruits = fruit => {}

forEach would already iterate over the fruits. You don’t want to repeat the logging for each vowel as you currently do. So no for loop needed here. Check the .includes()method.

And format code you post here, please: How do I format code in my posts?

This topic was automatically closed 41 days after the last reply. New replies are no longer allowed.