Falsy Bouncer prob

Tell us what’s happening:
output is [7,“ate”,false,9]
false isnt get deleted but this " " is why?
the output should be[7,“ate”,9]

Your code so far


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

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

Your browser information:

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

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

I suggest you take a look what happens in your code with http://pythontutor.com/javascript.html
It is not a good idea to splice the array on which you are iterating on

Also you should know that NaN === NaN is always false. But you may want to take a look at the ! (logical NOT) operator

https://javascript.info/logical-operators

1 Like

This is a common misunderstanding with the Falsy Bouncer. You can make a solution for this challenge which tests against a hardcoded list of falsy values, but it misses the point of the exercise. The concept of values being “falsy” isn’t something made up by Free Code Camp for the purposes of this challenge, but rather a quality that values have in programming languages. A value is falsy if the language treats the value as false when it is evaluated as a boolean. This means that Boolean(someFalsyValue) is false but also that we can use it in a conditional like if (someFalsyValue) . We can also talk about values being “truthy”. As I’m sure you’ve guessed, truthy values evaluate to true in a boolean of logical context.

if (someFalsyValue) {
    // this code block is not entered
}
else if (someTruthyValue) {
    // this code block is entered and executed
}
1 Like

I’ll take it a step further & suggest to never use splice.