Falsy Bouncer - how to get rid of NaN

same here: The code seems to miss the “null” value while finding all the others. What shell I do?

function bouncer(arr) {
  // Don't show a false ID to this bouncer.
    
  var filtered = arr.filter(faulty);
  return filtered;
}


function faulty(item)
{ 
      
  switch (item)
    {
      case false:
      case null:
      case 0:
      case "":
      case undefined:
      case NaN:
       return false;
      default:
        return true;
    }
}

bouncer([1, null, NaN, 2, undefined]);

Thank you but how do I get around it?

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

Thank you all. I solved the challenge but in the easy way. If you can take the time to explain why my code failed (the part that I put as a remark) I will appreciate it.

function bouncer(arr) {
  // Don't show a false ID to this bouncer.
    
  var filtered = arr.filter(faulty);
  // filtered.filter(spaecialFaulty); -- I tried to avoid using isNaN but failed
  return filtered;
}

function faulty(item)
{ 
      
  switch (item)
    {
      case false:
      case NaN:
      case null:
      case 0:
      case "":
      case undefined:
     
       return false;
      default:
        if (isNaN(item)){
            return item;
            }
        return true;
    }
}
/* function spaecialFaulty(item)
{
  return item;
}*/ 
bouncer([1, null, NaN, 2, undefined]);

The “filtered” array is declared on line above the call to “spaecialFaulty”

function bouncer(arr) {
// Don’t show a false ID to this bouncer.

var filtered = arr.filter(faulty);
filtered.filter(spaecialFaulty);
return filtered;
}

Yes I added one more filter function but it didn’t work

Never mind. You helped my through the challenge enough.