FAQ: Iterators - Choose the Right Iterator


#1

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!


#3

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
cities.
*(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.
thanks.


#4

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?


#5

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.


#6

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?


#7

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


#8

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?