FAQ: Iterators - The .filter() Method


#1

This community-built FAQ covers the “The .filter() Method” 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 The .filter() Method

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!


#2

For the second instruction, I used the following code:

const longFavoriteWorks = favoriteWords.filter(favoriteWords => favoriteWords.length > 7);

When I do a console.log, I see the output shown correctly, but the answer isn’t accepted. Am I doing something wrong, or am I doing this right and should this be accepted as a good answer?


#3

Here’s what I entered as a solution to Instruction #2:

const longFavoriteWords = favoriteWords.filter(word => {
  return word.length > 7;
});

You’ve got a type-o in the variable you declared (‘longFavoriteWorks’… ‘k’ instead of ‘d’) in addition to the format difference I used.

Hope that helps.


#4

Sometimes there are functions (iterations and other functions) with a “return” statement inside the code block and sometimes without the “return”, but codecademy didn’t explain it properly in my opinion (or maybe I forgot, although it’s only been a week, tops). In one of the early lessons on functions, when the only thing I wrote inside the function body was console.log(‘Whatever’), I additionally printed an “undefined” to the console, in the line under ‘Whatever’. And now, even if I don’t involve “return” in my functions, I DON’T receive “undefined”… I don’t understand at all when I need to use “return” and when I don’t need it. Can somebody please explain?

Example:

const longFavoriteWords = favoriteWords.filter(word => {
return word.length > 7;
});

vs.

const longFavoriteWords = favoriteWords.filter(word => word.length > 7);

They both deliver the same result. However, writing the following:

const longFavoriteWords = favoriteWords.filter(word => return word.length > 7);

doesn’t work as intended.

Is the “return” statement just something that is needed when writing a function with a multi-line block?


#5

The instructions read " The callback function is an arrow function has a single parameter, word ."

Shouldn’t they say " The callback function is uses fat arrow notation and the function has a single parameter, word ."?

Bad! They do not make any sense as stated?


#6

I think the return keyword is not necessary when the code is condensed into a single line and will throw an error if you use return as you did in the last example:

const longFavoriteWords = favoriteWords.filter(word => return word.length > 7);


#7

The lesson on .filter() states the following:
“The callback function for the .filter() method should return true or false depending on the element that is passed to it.”
However, for the question “Which of the following methods returns a boolean value?”, ‘.filter()’ is NOT a correct choice.
Is the definition of .filter() on page 4 NOT describing boolean values?

Additionally, the lesson for .findIndex() states the following:
" Calling .findIndex() on an array will return the index of the first element that evaluates to true in the callback function."
However, .findIndex() is NOT a choice for the question “Which of the following methods returns a boolean value?”
What about .findIndex() evaluating to true or false is NOT boolean?


#8

The return value of the .filter method is a list, determined by the callback. The callback is returning a boolean. But that is not what filter returns.

[1, 2, 3, 4, 5, 6, 7, 8, 9].filter(x => x % 2)

// [1, 3, 5, 7, 9]

The function, x => x % 2 will produce a 1 or a 0, which will be resolved to a boolean, true or false. Depending the outcome, the value of x will be inserted in the return list, or not. Recall that as an iterator, filter will iterate over the list it is called on.


#9

Screenshot%20(225)

Why is it “num” after .filter() instead of “randomNumbers”? In all other examples, the variable after .filter() is the same as the variable used for the original array.


#10

randomNumbers is the array we are drawing our data from, num is one piece of data at a time. .filter iterates over the data, tests it against the condition and assigns the values that pass the test to the output array.

What names we use is completely arbitrary, though one would hope they make sense and carry some meaning for the reader. The computer doesn’t care about meaning, but anyone reading the code will definitely mind if the variables are just like something out of a Dr. Seuss story.

num is an appropriate name for the parameter in the callback. It is singular, which imparts information in and of itself. It tells us what form the data takes, a num, short for number. Notice that the arrays arre pluralized? That tells us there is a quantity of numbers in each array (or could be, at least).