FAQ: Iterators - Choose the Right Iterator

The operative word in the instructions is, returns. What happens if we log,

array.forEach(callback)

Try it…

array = [1,2,3,4,5,6]
callback = x => console.log(x)
console.log(array.forEach(callback))
1
2
3
4
5
6
undefined
1 Like

Still not fixed. It’s already Feb 2021

this is how i understand, it returns undefined means it will print undefined if you use return keyword rather than console.log in forEach method, so just use console.log for forEach method, other method can return a values.
and other method can be saved in variable but not forEach. I could be wrong!!

I believe forEach() does not return anything, so it’s always “undefined”. I found this helpful:
“The forEach () method does not return anything ( undefined ). It simply calls a function provided in each element of its array. This call back has permission to change the Array that was called. It means that forEach () does not create a copy of the original Array to interact over it.” I got it from this link:

2 Likes

I think this is right! I think it might be easier to understand if you create a new array using the .forEach() on the cities array, and then return the value of the new array

//  Choose a method that will return undefined
const moreCities = cities.forEach(city => console.log('Have you visited ' + city + '?'));
console.log(moreCities); //Returns undefined

The returned value here will be undefined, because the .forEach() method iterator does not create a new array, but simply enacts the callback function on each array.

Do correct me if I’m wrong though?

1 Like

April 2021 and still not fixed. I reported the bug though :woman_shrugging:

This explains what they meant in their question, but this is a horrible way of teaching. Why are we supposed to use a method that returns undefined but then the code example uses console.log to actually display every city in our array? It’s highly misleading and confusing for everyone new to these concepts. Unfortunately this is not the first or second time I encountered these misleading exercises. Also, why is this an age-old issue that comes up over and over again in this thread but nobody from Codecademy feels like it’s worth responding? Overall this is really, really disappointing.

2 Likes

I was really confused by this one, especially the comment in the code

And the hint

was not helpful at all.

I may have skipped the part of the instruction

I have been too focused on the undefined bit and it had left me confounded for a while.

While the instructions may be technically correct, I think it is quite bad to confuse people while they are learning and lead them the wrong path.

So did you solve this? Did you read the documentation linked to in the narrative?

2 Likes

Yeah, I went to the cheatsheet and simply tested one method after another… that reminds me, that the cheatsheet for the lesson does not have any mention of undefined either.
I then read this forum and because I was not the only one confused by this task, I went to add my opinion on the matter.

So may we assume then that you did not read the documentation, as suggested?

1 Like

I did read the documentation in the previous steps, but I did not remembered it all;)

That is a good reason to bookmark so you can go back and review. The answer is right there in the docs. Of all the iterators we learned about, only one of them returns, undefined. Did you find which one?

1 Like

I would argue that it is also a good reason to update the cheatsheet and the lesson. Otherwise, what is the point of having a link to cheatsheet that is missing crucial piece of information? You could replace it with a link to the documentation instead.
I mean, you do not go to language classes to be told that the answer is in the dictionary. It is given, but listing through a dictionary is not a very effective way to learn a language. Ability to use dictionary is not a bad skill, it is handy; likewise, it is great to be able to use the documentation here. Still, it is weird to see all the confused people here and hear the advice to read the documentation, when the issue is the rather confusing information provided in the test and misleading hints that do not help to solve it.

1 Like

The narrative in lesson 7 is as follows:

There are many additional built-in array methods, a complete list of which is on the MDN’s Array iteration methods page.

The documentation for each method contains several sections:

  1. A short definition.
  2. A block with the correct syntax for using the method.
  3. A list of parameters the method accepts or requires.
  4. The return value of the function.
  5. An extended description.
  6. Examples of the method’s use.
  7. Other additional information.

https://www.codecademy.com/paths/web-development/tracks/web-dev-js-arrays-loops-objects/modules/learn-javascript-iterators/lessons/javascript-iterators/exercises/documentation

1 Like

The return value of forEach is actually ‘undefined’, so saying one should replace the method for one that returns ‘undefined’ isn’t wrong. It is just very confusing.

I think the trouble is that you could use a lot of other functions to achieve the same effect of printing all the cities, but they wanted you to specifically use forEach.

1 Like

Thank you! I was literally dying with this exercise, I didn’t understand it at all!
I got anxious every time I opened CodeCademy because of this exercise

This is still a problem and as far as i can see its now been 2 years

Aaaaaahhhh… now I understand.

It prints the questions for the cities but still does return undefined

The thing is, the return is not shown on the console as this was not part of the code.

3 Likes

Exactly. It can be a little difficult to grasp at first, but returning and printing are in no way related at all. Printing is merely so we humans can see what is going on. Seeing what is going on is an important step for us to understand exactly what our code is doing, and is a great asset when it comes to debugging. To see the return value of forEach() we could do something like this:

// Should print each number in the array multiplied by 10 to the console. const someName = [1, 2, 3, 4, 5].forEach(number => console.log(number * 10)); // Now to see what value was returned: console.log(`Here's the return value: ${someName}`);
2 Likes