Falsy Bouncer help needed!

Tell us what’s happening:
Can anyone tell me why this code doesn’t pass the test?

Your code so far

function bouncer(arr) {
  // Don't show a false ID to this bouncer.
  return arr.filter((sub)=>sub!=false);
console.log(bouncer([7, "ate", "", false, 9]));

Your browser information:

User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.102 Safari/537.36.

Link to the challenge:

Your code only filters false, therefore it fails the test cases where other falsy values like NaN or undefined are passed. Your code must catch and remove all the falsy values: false , null , 0 , "" , undefined , and NaN.
You are on the write path, try rewriting your code in accordance to the above information.
More about falsy
P.S. When you are confused, it always helps to take a look at the test cases.
Hope this helps.

Hi @danielchae ,

you don’t need this => sub!=false.

remember that .filter() already is set to return a value that meets the criteria of your condition within your filter function which means it already returns a ‘truthy’ value so you don’t need to set your array into “not false” for the sake of returning the “truthy” values.

Values like undefined, NaN, " ", null, [ ] or 0 are recognized by JS engine as values == false or “falsy” values so this means that .filter() will not return these values unless you explicitly tell it to.

I hope that helps.


Thanks for the comment @AdityaVT . I little bit of doubt here.
You mentioned that I filtered only with false but not with other false values such as null, 0, “”, undefined. I did it intentionally with non-equality operator (!=) of non strict type. As I learned from previous lessons, this operator has to change the compared values to common type so that they can be compared.
I would agree with you if I used “!==” strict non-equality operator.
Did I get something wrong?

Again, thanks for the response!

You are right, I overlooked that fact. But still your code only matches for false, 0 and "", it ignores undefined, NaN and null. This is because of the peculiarities of the ‘==’ and ‘!=’ operators, when these are applied to null or undefined type conversion does not happen. Therefore null equals only to null or undefined, and does not equal anything else.
null == 0 => false, null is not converted to 0
null == null => true
undefined == undefined => true
null == undefined => true
and also NaN does not equal to anything, not even itself.
To make your code work you can type convert all the elements to boolean, this would change all the falsy values to false, and thus your code would work.
But as @iamcris pointed out, filter() already has an inbuilt method to weed out false values, so simply returning each element in the filter() function would also work.

@AdityaVT Thanks for the answer! Maybe I slightly misunderstood the concept of loose equality operator.

It changes values to common type so that they can be compared but that doesn’t guarantee that a truthy/falsy value will be equal to the other truthy value/falsy value.

For example, If I compare two truthy values of different types such as “4” == 5, they can be compared because they are changed to the common type but it doesn’t mean that they are equal.

On the top of this, some values are just cannot be compared. For example, if I compare “a” == 5, it will return false because they are just different values.

Anyway, your comment helped me a lot! Thanks

1 Like

@iamcris This is the solved the issue. Thank you very much!