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 () 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 () below!
Agree with a comment or answer? Like () to up-vote the contribution!
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?
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?
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:
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?
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.
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.
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.
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).
const words = [‘chair’, ‘music’, ‘pillow’, ‘brick’, ‘pen’, ‘door’];
const shortWords = words.filter(word => {
return word.length < 6;
});
words is an array that contains string elements.
const shortWords = declares a new variable that will store the returned array from invoking .filter().
****SHOULDN’T IT SAY " STOP" INSTEAD OF “STORE?” - thank you
The Array.filter() method returns a new array of values that satisfy the predicate in the callback. ‘Store’ would be a term that more closely describes this.
let shortWords = words.filter(word => word.length < 6)
I wondered this myself. Why is ‘num’ the piece throw in there? Does the computer make the assumption were talking about numbers in the array versus say if I threw in another arbitrary variable like ‘try’ or ‘whatever’, instead of the variable ‘randomNumbers’?
When i replace ‘num’ with another arbitrary variable like ‘try’ , it returns an error. So it leaves me to assume the variable ‘num’ has some inherent value in the system, since num has not been declared as any other value in this iterator lesson? Is this not correct?
ANOTHER EXAMPLE would be the ‘word’ variable throw in on the previous exercise on this lesson:
Why is ‘word’ used and not ‘shortWords’ ? ’ if I try to replace ‘word’ with another random variable, it returns an error. Does ‘word’ have an inherent value in this context as well that was not explained, or is it just another arbitrary word? Does ‘word’ assume the value of ‘shortWords’?
Not as a whole. It is only a temporary variable, taking one word at a time from the larger list. What we call it does not matter. However, if you replace the parameter, be sure to replace it in the object.length expression.
Why can’t I assign some other random name as that temporary variable? Why does only ‘words’ work in this equation without returning error? I understand why ‘words’ is used, it makes sense in context of this code, but I just dont understand why other random variable names dont work. Which is why I’m asking, is there some other value to ‘word’ im not aware of? Or is the fact the computer isn’t letting me input some other random variable name just a computer error?
let shortWords = words.filter(word => word.length < 6)
shortWords and words are defined data structures. The fact they are both pluralized speaks of there being some or many, not only one item in them.
The temporary variable is chosen as the singular of the list it is iterating.
words => word
plural singular
When viewed in such a way the code is readable and understandable. What other variable would make more sense?
As for choosing other names, we could write:
shortWords = words.filter(x => x.length < 6)
and get the same result. There is no error.
Is it the words variable you are trying to change? Then be sure the data structure it refers to is given the same name you wish to use in this statement.
Computer errors only occur when something is encountered that cannot be found or a value is incorrect for its usage, such as a TypeError. It is our job to be sure these sorts of errors do not happen, and if they do, to trace them back to their cause.
Perhaps it’s the Codecademy exercise interface that’s throwing you an error, not your code. Try your code in a program like Visual Studio Code and see if it works. I’m betting it will.