Falsy bouncer doesn't work for ""

Tell us what’s happening:
I need to remove all falsy values from an array. My code works except for the empty string… It still returns “” even when I wrote the if statement like this: if (arr[i] | arr[i] === “”)
I don’t understand why this happens. Shouldn’t an empty string return false?
thank you :slight_smile:
Your code so far


function bouncer(arr) {
let newArr = [];
for (let i = 0; i < arr.length; i++){
  if (arr[i]);{
    newArr.push(arr[i]);
  }
}
return newArr;
}

console.log(bouncer([7, "ate", "", false, 9]));
console.log(bouncer([false, null, 0, NaN, undefined, ""]));

Your browser information:

User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.61 Safari/537.36.

Challenge: Falsy Bouncer

Link to the challenge:

Hey @carmen_zavala

What is your if block trying to test? Because it’s not testing for anything.

In your code here too, there is a ; (semi-colon) after your parentheses, this can cause it to fail to.

You have a stray semicolon in the middle of your if block. Remove that and you should be a happy camper.

2 Likes

@Catalactics the if statement is testing the “truthiness” of the value. The contents of an if condition are evaluated as booleans.

2 Likes

Got it… Thanks for correcting me, I crossed part of my post.

Thank you! your right, I made a mistake. The if statement evaluates to true or false. If it’s a falsy it should return false, otherwise is true.

1 Like

Thanks @ArielLeslie :smiley: