2.7 | .forEach()


#1

My code

let fruits = ['mango', 'papaya', 'pineapple', 'apple'];

// Iterate over fruits below
fruits.forEach(function(fruit) {
	console.log('I want to eat a ' + fruit);
});

The console isn’t raising any errors, so I don’t understand why I’m not passing the first objective which is:

  1. Iterate over the fruits array to log I want to eat a plus the name of each fruit to the console. For example, I want to eat a mango.
    Use function(parameter) syntax for this step (not arrow function => syntax).

Can someone explain what I’m doing wrong?


#2

Please remember to include a link when posting questions about exercises. Thanks.

Your code looks fine. Have your tried with a template literal?

console.log(`I want to eat a ${fruit}.`)

Also, is there an expected full stop, perhaps?


#3

Tried a template literal, didn’t work, and there isn’t an expected full stop


#4

Link to exercise, please?


#5

https://www.codecademy.com/courses/learn-javascript-iterators/lessons/javascript-iterators/exercises/for-each?action=lesson_resume&course_redirect=introduction-to-javascript


#6

Maybe try the arrow function syntax from step 2 and see if the problem goes away.

My lesson is already done so I can’t tell what will or will not pass the SCT. Here is what I had…

fruits.forEach(fruit => console.log(`I want to eat a ${fruit}`));

#7

Extra Study

Playing with the code after completing a lesson is a nice segue and breather, and helps us to absorb the new information enough to get comfortable with it.

Here is a little grammar checker (run in the lesson editor)…

fruits.forEach(fruit => console.log(`I want to eat \
${'aeiou'.indexOf(fruit[0].toLowerCase()) < 0 ? 'a' : 'an'} ${fruit}.`));
I want to eat a mango.
I want to eat a papaya.
I want to eat a pineapple.
I want to eat an apple.


#8

Ok thanks for your help


#9

Did you make it through the lesson okay?


#10

I’m facing the same issue. i also tried your instructions but it’s still not working

https://www.codecademy.com/courses/learn-javascript-iterators/lessons/javascript-iterators/exercises/for-each?action=lesson_resume&course_redirect=introduction-to-javascript

Any help would be highly appreciated