Codecademy Forums

FAQ: Iterators - Choose the Right Iterator

This community-built FAQ covers the “Choose the Right Iterator” exercise from the lesson “Iterators”.

Paths and Courses
This exercise can be found in the following Codecademy content:

Web Development

Introduction To JavaScript

FAQs on the exercise Choose the Right Iterator

There are currently no frequently asked questions associated with this exercise – that’s where you come in! You can contribute to this section by offering your own questions, answers, or clarifications on this exercise. Ask or answer a question by clicking reply (reply) below.

If you’ve had an “aha” moment about the concepts, formatting, syntax, or anything else with this exercise, consider sharing those insights! Teaching others and answering their questions is one of the best ways to learn and stay sharp.

Join the Discussion. Help a fellow learner on their journey.

Ask or answer a question about this exercise by clicking reply (reply) below!

Agree with a comment or answer? Like (like) to up-vote the contribution!

Need broader help or resources? Head here.

Looking for motivation to keep learning? Join our wider discussions.

Learn more about how to use this guide.

Found a bug? Report it!

Have a question about your account or billing? Reach out to our customer support team!

None of the above? Find out where to ask other questions here!

i was stuck with this part, and had to really look at the sentence to understand what it was telling me to do.
So i decided to write some notes down, and share it to other codecademy members if they also stuck.

1.Replace the word method in the first method call with a method that will do something to each of the values in the array and return undefined .
const cities = [‘Orlando’, ‘Dubai’, ‘Edinburgh’, ‘Chennai’, ‘Accra’, ‘Denver’, ‘Eskisehir’, ‘Medellin’, ‘Yokohama’];
// Choose a method that will return undefined
*(city => console.log('Have you visited ’ + city + ‘?’));

Notes. For each of the city in the cities array, the console.log will run the string “Have you visited” with the city.

2.In the second method call, replace the word method with a method that will return a new array with only those elements longer than 7 characters.
// Choose a method that will return a new array
const longCities = cities.
*(city => city.length > 7);

Notes. filtering the cities in the array with length of more than 7, and save it under longCities.

3.In the third method call, replace the word method with a method that take an array that contains multiple values and returns a single value.
// Choose a method that will return a single value
const word = cities.
*((acc, currVal) => {
return acc + currVal[0]
}, “C”);

Notes. reducing each value to just 1 character, thus[0] index, on the whole values in the string, and concatenate with a “C”

4.In the fourth method call, replace the word method with a method that will return a new array of numbers returned from the function.
// Choose a method that will return a new array
const smallerNums = nums.
*(num => num - 5);

Notes. search every value in the array, and pass value to smallerNums.

edit. thank you @pyjumper62515, for pointing this error. an updated link has been posted now.


It is my understanding that the fourth step is asking for a method that returns an entire new array. I see nothing in the documentation of the .every() method that suggests it does this. It returns a boolean value. In the examples I also do not see any of its uses being stored into a new array variable.

It is to my understanding that the .map() method does create a new array, and would be properly used in the given situation.

am I missing something?


hey @pyjumper62515,
you are correct. i was reading on some prototypes, and somehow ended up attaching .every into it.
I better update them now, thanks for pointing that out.

I am very confused about the first step of this exercise. It tells us to use a method that will return undefined. The solution was to use .forEach() to log the sentence for each city in the array. This result is not undefined as the step asks. What am I missing?


I am also confused by this and not clear as to why using the method ‘forEach’ should return undefined.


Agreed, this makes no sense to me. My first thought was “will do something to each of the values” was a clue to use .forEach, but I knew there was no way this would return undefined, so I tried many other methods until I gave up and checked the solution. The answer being .forEach totally baffled me. Can anyone explain this?

1 Like

I was also stuck on this and had to look at the thread to figure out the solution was forEach(). If you look at the documentation for the forEach() method, it says that the return value is undefined. forEach is just a method meant to do something with each element (in this case print a sentence), but not return anything.


my question about this array:

const cities = ['Orlando', 'Dubai', 'Edinburgh', 'Chennai', 'Accra', 'Denver', 'Eskisehir', 'Medellin', 'Yokohama'];

Why/how does

const word = cities.reduce((acc, currVal) => {
  return acc + currVal[0]
}, "C");

put the “C” character in front of the rest of the returned values (to print CODECADEMY)?

.reduce returns a single value, in this case a string made up of the first letters of a number of strings.

The return on each iteration grows by one letter until the whole input array has been gone through. The "C" in the argument is the default starting value for the accumulator.

CO          // first iteration

Note how the first iteration results in CO.


Is there a certain trick to remembering which is which or do we just have to memorize them?

1 Like

Your Science teacher will tell you not to memorize formulae, but learn to recognize the concepts when expressed in an equation. Usually we begin learning general scientific concepts in junior high (middle school? Fifth Form?) in scalar terms (no direction). Consider,

d = v * t

F = m * a

W = F * d

v = a * t

Recognize any of these? Remember, at this stage there is no sense of direction, just the basic relationships.

What does rate times time give? What does acceleration * times time give? What does force times distance give? What does mass times accelertion give?

See what I mean? Focus on the concepts and how they relate, and you will easily remember what their relationship defines.

1 Like

On number 4 , In the fourth method call, replace the word method with a method that will return a new array of numbers returned from the function., I am having difficultty figuring this one out . Any help out there?

How many iterators return a new array?

forEach?  no
reduce?   no
for..of?  no
every?    no
 some?    no

map?      yes
filter?   yes

Now ask yourself, does the callback function make a comparison? Or does it perfom an arithmetic operation? This should help you decide which of the above two to use.

1 Like

Part with code cities.forEach(city => console.log('Have you visited ' + city + '?')); wasn’t really clear to me, because cities.find(city => console.log('Have you visited ' + city + '?'));still returns undefined in this particular case. Can you fix it please?

That’s because the .find() method requires a boolean test condition. It returns the value of the first element that satisfies the condition. Your callback function city is not a test, it is instead a function that is applied to each element of the array cities.

When you execute since there’s no test that could be true or false, it returns undefined. Now it depends on what you want to do. To correctly use find this should work:

const findDCity = cities.find(city => city[0] === "D");