Falsy bouncer: My solution please help

Hi. I am have attempted the falsy Bouncer, but I have a hard time dealing with the falsy NaN. I have tried to use isNaN and other options but still nothing. Please can someone help me with this very beginner solution.

Can it be fixed?

function bouncer(arr) {
  // Don't show a false ID to this bouncer.
  
 var falsyValues = [false, null, 0, "", undefined, NaN];
  var array =[];
  array = arr.filter(function (value){
//     for (var i = 0; i < falsyValues.length; i++){
    if(value === false && value !== "" && value !== null && value !== 0 && value !== undefined ){
      return  arr; 
    }
    
//     }
             
             });
  
  return array;
}

isNaN doesn’t work as you would expect. It allows coercion, so if you passed it, say, 'hello', it will coerce it to a number and come up with NaN, so it returns true.

There’s a stricter Number.isNaN function that only works with numbers and returns true just for actual NaN values.

Thanks. You helped me to understand the filter() function better;

my solution:

function bouncer(arr) {
// Don’t show a false ID to this bouncer.

var array =[];
array = arr.filter(function (value){
return value;

         });

return array;
}

1 Like
**********Falsy Bouncer Solution********

function bouncer(arr) {

function falsyValues(val){
return val;
}
return arr.filter(falsyValues);
}

hello i am having trouble understanding how your code is working. could you please explain what is happening.

sorry for not clarifying. I was talking about ahsanwaseem’s code, but after studying it for a while it all connected. thank you for replying though!