The .forEach() Method lesson

I was curious as to why when I write the function how I originally wrote it:
let fruitCall = (fruits) => {
console.log('I want to eat a ’ + fruits);
}

fruits.forEach(fruitCall);

It shows up as intended with each part of the array being displayed at the end of the sentence:
I want to eat a mango
I want to eat a papaya
I want to eat a pineapple
I want to eat a apple

but when I write it how it was written in the lesson which is like this:

fruits.forEach(fruitCall => {
console.log('I want to eat a ’ + fruits)});

The result is:
I want to eat a mango,papaya,pineapple,apple
I want to eat a mango,papaya,pineapple,apple
I want to eat a mango,papaya,pineapple,apple
I want to eat a mango,papaya,pineapple,apple

I don’t understand why it doesn’t print each string separately like the first function I wrote.

Am I missing something or writing something wrong?

Hey @uglybanter, welcome to the forums!

That’s because you are using the wrong variable.

Here, you loop through the array fruits but instead of printing each element, you print the array four times.

Thanks for having me!

And thank you! I was using the array instead of the declared variable. I get it now

1 Like