FAQ: Iterators - Choose the Right Iterator

I’m confused about syntax as to when to use curly braces { } in creating a callback function and when not. In the exercise
“Choosing the right iterator” all but one of the solutions do not use curly braces. The on that does (using the .reduce method) has the { } - is this because this particular callback has a “return” as part of its callback process whereas the others don’t? Thanks, KdL

Agree with so many previous students - the phrasing of the step 1 is confusing and misleading, and the suggestions to read the documentation is not helping - I have read it and tried to apply to this exercise with no success.
It is easy to blame the learners for being stupid or lazy - but please, this is not the case here. I join the others - PLEASE re-phrase this exercise!!!


Hey, I don’t know whether this will help for anyone stuck on the first step of this exercise.

If my understanding is correct - @mtf is right, and it took me longer than I’d like to admit to grasp.
If you switch the .method for .forEach(), the code runs and you’ll get the list of “have you visited [city}?”

You get that because the function being utilised within is console.log which is printing the sentences, the method .forEach isn’t returning anything to the console.

As mtf said, the phrasing is right because the method isn’t returning anything, it’s the console.log statement inside the pre-written code that’s getting you your output back in the console?

Feel free to jump in if I’ve misunderstood something (I’m pretty new to Javascript and coding in general), but the exercise came up with a tick and it let me move on?

I totally get why people are confused though, it’s something I had to read over and over till it clicked.
Hope this helps the next person from tearing their hair out, aha :sweat_smile:

1 Like

I think the main issue here might be that people are interpreting the word ‘return’ generally rather than specifically. I know I did. So interpreting a console.log to be returning something, which in general English terms, it does, but is not an actual return in Javascript terms.

1 Like

Thank you! I get the .forEvery() iterator for the first one now, but I was really struggling to find it when I was searching. I think it’s because very few people will call .forEvery() with the purpose of expressly getting an undefined value in return, so why would MDN signpost this as a feature! Weirdly written, and the first time I have seen the “hint” section be actively unhelpful!

Really appreicate the time you took to write this up.

It’s almost 2023. .forEach returns undefined. Is your confusion as to why console.log shows the city?

I asked ChatGPT.

Calling .forEach() on an array returns undefined.

That is correct. The forEach() method is used to execute a function on each element in an array, but it does not return a new value or modify the original array. It simply calls the provided function for each element in the array and does not return any value.

Really? Like you needed to? The narrative describes all of this, which is where ChatGPT probably got it from. Are you in this course, or not? Take it in, then re-examine it using those outside platforms.

1 Like

We are being encouraged to use the documentation to look things up, so there were two possible ways to find the answer, here… it’s just a matter of being patient (with ourselves) and doing the work.

For anyone still stuck, these are the most helpful answers (for me).

It’s an important bit of nuance that I was totally skimming over until reading every post in this thread. Whilst it’s true that it’s all there in the materials, it might have helped to highlight or explain it a little bit more when first mentioned to make learners more likely to pick it up. I certainly missed it, and it makes this subsequent lesson (naturally) impossible to understand if you didn’t pick it up in the first place.

Thanks to the two posters quoted above for the illumination.

There’s nothing to interpret. We just have to learn what return does. console.log prints the arguments passed to it to the console, and returns undefined. It is absolutely an actual return in JavaScript terms.

1 Like

I agree too !
I tried .reduce() because I was sure it would return undefined since it would try to sum the words.
It did return undefined! but not for the first city

I couldn’t figure out how to find the right method requested by step one just through the instructions so I tried working backward. Based on working backwards, I’m getting the impression that I was supposed to use iterative as the keyword which would lead me to the sections Array methods and empty slots and Iterative methods then read the documentation of every method until I found the methods that return undefined. My concern is reading the documentation for all the methods in a section until I find the one that returns undefined feels like working hard not smart. How could I work smart instead?

1 Like

Still this issue have not been fixed :pensive:

And for me the most confusing part is that there’s a lot of "Have you visited"s printed, but no ‘undefined’ on my console. In the error message also no ‘return undefined’, so how was i supposed to know that this was the right method?
I have searched in the documentation, understandably we need to learn to use that. There’s more methods that could possibly return undefined. It’s a very unclear assignment. As a former professional in Education this was a poorly written assignment.

There is nothing to fix. Just because you don’t see “undefined” in the console doesn’t mean it didn’t return “undefined”. It did return “undefined”, we just didn’t do anything with the return value. .forEach is the method of choice here because it does something to each of the elements in the array (logs them to the console) and returns “undefined” (which, again, we did nothing with). You can verify for yourself that .forEach returned “undefined” by assigning .forEach’s return value and logging it to the console. I suggest reading this entire thread with a focus on @mtf 's replies. In fact, I’d suggest always focusing on his posts whenever reading a codecademy thread, lol.

it seems to me that a lot of people are confusing ‘return’ with what is being logged to the console. they are two different things.