My code is working ACCIDENTALLY. But I don’t understand why.
What does the function passed inside the filter method do?
function bouncer(arr) {
let x= arr.filter(num=>num)
console.log(x)
return x;
}
bouncer([7, "ate", "", false, 9]);
Challenge: Basic Algorithm Scripting - Falsy Bouncer
Link to the challenge:
I guess MDN article has better explanations than something I can provide
Ask more questions please, if after reading you will still have any.
ilenia
3
Filter keeps a value when the callback function returns true/a truthy value, and reject a value when the callback function returns false/a false value
So, depending on what num
is (and that is a pretty bad variable name, the values can be anything, not just numbers)…
Thanks for the suggestion for the variable name. I will keep that in mind.
But in this case, if I write
arr.filter(num=>num==true)
this doesn’t work. Why?
ilenia
6
Because not all truthy values are equal to true
Thank you.
I have read it, but failed to understand .
Ow! Now I got it. Thanks a lot for the support.
And I am really sorry if I broke any rules while posting this. This is my first post in the forum.
Thanks again.
ilenia
9
Did anyone say anything about breaking rules?
No , nobody said anything about breaking rules.
system
Closed
11
This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.