FAQ: Iterators - Choose the Right Iterator

Nothing to fix. All that’s missing is the demonstration of facts.

    a = cities.forEach(...)

    console.log(a)    // undefined
1 Like

Hi, I am confused with a few items in this exercise:

Step 1) I thought that the “forEach” returned the value of the city in that index. How does it returns undefined?
Step 5) nums.some(num => num < 0); This is supposed to return a boolean value but it is not printing true or false to the console. I tried to adding console.log or return but it runs with an error.

forEach doesn’t return anything, hence the default, undefined. We use forEach to perform an action on all the elements in an array.

upper = []
lower = ['a', 'b', 'c', 'd', 'e', 'f']
lower.forEach(function(x) { 
    this.push(x.toUpperCase())
}, upper)
console.log(upper)    // ["A", "B", "C", "D", "E", "F"]
nums = [-1, 2, -3, 4, -5, 6, -7]
negs = nums.some(x => x < 0)
console.log(negs)    // true
1 Like

thank you for fast reply and the clarification.

1 Like

August 2020, still now idea what’s the deal with that first step…

There is a link to the MDN page that describes all the iterators. If we read their descriptions carefully we find the answer that question is looking for.

array.forEach()

returns, undefined.

I disagree that there is “nothing to fix”. I too was confused what the question was asking me to do, and thought it required me to use a method that would make the output in the console say “undefined” for every item in the array. I had to find this thread to finally learn what the answer was meant to be. And considering how many other people are coming to the same wrong conclusion, something definitely needs to be fixed in how the question is asked.

That may trace back to them not visiting the page in question and reading it. As pointed out, already… The answer is right there.

It really doesn’t matter though how much “the answer is right there” if the question is leading people to search in the wrong place. Of course the answer is obvious when you already know it. But when you don’t, it’s not necessarily so straightforward, looking at how many people come here with exactly the same problem.

See, to me, the way the question is phrased makes it seem like it wants me to use a method that makes the whole list show up in the console as “undefined”, which would be a “wrong” answer, but as it starts off by saying “don’t worry about any error messages, we’ll fix those later,” you could reasonably expect it to require “wrong” answers that may yield errors, to be fixed later with the correct ones (wouldn’t be the first time that happened in these courses). That’s why I was ignoring forEach() as a correct answer—I wasn’t looking for the “correct” answer.

All I’m saying is that I found the answer when I read the page the lesson directed us to. The wording matched the question. Perhaps somebody from curriculum will spot this topic and review the exercise. Not sure anything will change, though. They couldn’t spell it out any better without giving the answer away. We need to read documentation, no matter how well we think we know the material. I don’t know anybody who doesn’t have it bookmarked and regular visit it.