NEED Help Falsy Bouncer

NEED Help Falsy Bouncer
0

#1

Tell us what’s happening:

Your code so far


function bouncer(arr) {
  // Don't show a false ID to this bouncer.
    console.log(arr)
  for(let i=0;i<arr.length;i++){
    if (arr[i] == false || arr[i] == null || arr[i] == 0 || arr[i] == "" || arr[i] == NaN || arr[i] == undefined){
      arr.splice(i,1);
    }
  }
  console.log(arr)
  return arr;
}

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

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 6.3; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/68.0.3440.75 Safari/537.36.

Link to the challenge:
https://learn.freecodecamp.org/javascript-algorithms-and-data-structures/basic-algorithm-scripting/falsy-bouncer


#2

instead of all those or statements
try this
if (!arr[i]) {

what it does is it gives true if arr[i] is not a truthy
(so all those values you are trying to explicitly check for are not truthy)


#3

but what is wrong with the code i wrote above


#4

Two things:

In JavaScript, nothing is equal to NaN. NaN is not even equal to itself. You can use Number.isNaN(var) to check if a variable is NaN.

Each time you splice an array, it gets shorter, but you haven’t done anything to adjust i accordingly.


#5

When you are so explicit with the falsy values, you miss the point of the challenge. which is to show that things that have certain values will evaluate to false in a boolean context.


#6

can you elaborate please


#7

I’ll give you an example

var aNumber = 0;
if(aNumber) {
  return "truthy";
} else {
  return "falsy";
}
//output will give you "falsy"

#8

just to add a bit more to what @zapcannon99 is saying,

true and false are both boolean values but
a ‘falsy’ is value that is not Boolean but still evaluates to false (in boolean context), eg. 0
a ‘truthy’ is a value is not Boolean but still evaluates to true (in boolean context), eg. 1


#9
  • Removing all falsy values from an array = return all truthy values.
  • The filter() method creates a new array with all elements that pass the test implemented by the provided function. (MDN definition)
  • If .filter() method is properly implemented, you’ll return only those values which return true in the Boolean context – thus solving the challenge in one line