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!!!

2 Likes

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:

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.

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.