Falsy Bouncer Challenge

Hello all,

I’m trying to better understand how filter() works in JS.
Here my attempt at pulling only true values from an array:

function bouncer(arr) {
  // Don't show a false ID to this bouncer.
  arr.filter(function(val){
  return val !== false || null || "" || undefined || NaN; 
  }); 
  
  return arr;
}

bouncer([7, "ate", "", false, 9]);

Any guidance is appreciated.

Also, double check the documentation on filter function and see what filter returns.
Hint: Does filter mutate the array or return an array?

Thanks Reggie01 and rmdawson71.

Still a bit lost, but I’ll keep reading the documentation and try again tomorrow.

function bouncer(arr) {
  // Don't show a false ID to this bouncer.
  arr.filter(function(val){
    
    if(val === false){
      return true;
    }
    else if(val === null){
      return true;
    }
    else if(val === 0){
      return true;
    }
    else if(val === undefined){
      return true;
    }
    else if(isNaN(val)){
      return true;
    }
    else if(""){
      return true;
    }
    else{
      return false;
    }
     
  }); 
  
  return arr;
}

bouncer([7, "ate", "", false, 9]);

Firstly, Array.prototype.filter() returns a new array. It does not mutate the working copy. A simple return arr.filter(...) should be enough.

Secondly, read up this definition on Truthy values, that should point out how to check for the values you need.

Welp. I’m losing my damn mind over this one. Even though the comparisons for array items '' and false evaluate as false, return arr.filter still returns the whole array, despite @wesleywerner advice. Any thoughts? thanks!

function bouncer (arr) {
  return arr.filter(function (item) {
   return item !== false || item !== null || item !== 0 || item !== '' || item !== undefined || item !== isNaN
  })
}

bouncer([7, 'ate', '', false, 9])

thanks @rmdawson71 ! don’t know how I missed that…