Understanding a Falsy Bouncer

Understanding a Falsy Bouncer
0

#1

The challenge is https://www.freecodecamp.org/challenges/falsy-bouncer

Can someone please explain what’s wrong with my code?

function bouncer(arr) {
     var newArray = arr.filter(function(element){
     return element != Boolean();
  });
  return newArray;
}
bouncer([7, "ate", "", false, 9, null, false, 0, NaN, undefined]);

It works but it only works for “false” and “0”. I understand that I am overcomplicating myself by doing this when I can simply just use the code below.

function bouncer(arr) {
     var newArray = arr.filter(Boolean);
  return newArray;
}

However, I am interested in finding what’s the problem.

Thanks in advance!


#2

Boolean() function without the parameter always returns false.
As arr.filter method expects value returned from callback to be Boolean, it will automatically convert it.

     var newArray = arr.filter(function(element){
       return element;
    });