Falsy Bouncer Challenge

Falsy Bouncer Challenge
0.0 0

#1

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.


#2

Hi Drustin2,

You actually have two issues:

  1. When you use multiple OR operators ( || ), each condition between the || needs to be compared with something. In this case you are trying to check val !== all the values, but each value should be compared to val.

  2. Remember, filter will only work when comparisons evaluate to true . Put the following before the return statement in the filter, so you can see what each of these comparisons actually evaluate to. You may be surprised what the evaluations are.

console.log(val !== false);
console.log(val !== null);
console.log(val !== "");
console.log(val !== undefined);
console.log()val !== Nan);

Hopefully, I have pointed you in the right direction. If you have any more questions, just let us know.


#3

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


#4

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]);

#5

Reggie01 has given you a big hint. When I said multiple comparisons, I meant something more like the following. I am using a different if statement with a variable named x just to show you an example:

if (x === '2' || x !=== 9 || x === null) 

#6

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.


#7

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])

#8

@jmbothe - You are using || (or), so remembering that only one OR (in a sequence of ORs) has to be true for the entire statement to be true, your filter will capture all the values of the given array.

Below is how your filter iterates through the given array:
1st element = 7, so the item !== false evaluates to true, so 7 is filtered,
2nd element = ‘ate’, so item !== false evaluates to true, so ‘ate’ is filtered,
3rd element = ‘’, so item !== false evaluates to true, so ‘’ is filtered,
4th element = false, so item !== null evaluates to true, so false is filtered,
5th element = 9, so item !== false evaluates to true, so 9 is filtered.


#9

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