Basic Algorithm Scripting - Falsy Bouncer

Tell us what’s happening:
Hi, I’m trying to resolve the challenge with a switch, but don’t work in the case of NaN. I’ll trying solve it with a nested if else statements but I want to understand why javascript don’t take the case NaN:
Anyone can help me.

Very thanks

Your code so far

function bouncer(arr) {
  let newArray = [];
  for (let value in arr) {
    switch (arr[value]) {
      case false:
      case null:
      case 0:
      case "":
      case undefined:
      case NaN:
        break;
      default:
        newArray.push(arr[value]);
    }
  }
  return newArray;
}

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/105.0.0.0 Safari/537.36 Edg/105.0.1343.53

Challenge: Basic Algorithm Scripting - Falsy Bouncer

Link to the challenge:

NaN === NaN

This evaluates to false

Yes, all first blocks of cases only break the switch ignoring the element.
if true, that’s the idea.
And if don’t break in the first cases go to default and push the element.

With if and isNaN() I have problems too.

function bouncer(arr) {
  let newArray = [];
  for (let value in arr){
    if (!isNaN(arr[value])){
      console.log(arr[value]);
    }
  }
  return newArray;
}

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

Yes, all first blocks of cases only break the switch ignoring the element if the condition is true, that’s the idea.
And if don’t break in the first cases go to default and push the element.

With if and isNaN() I have problems too.
Don’t print ‘ate’ How i can test NaN without interfering with others checks?

:sweat:

function bouncer(arr) {
  let newArray = [];
  for (let value in arr){
    if (arr[value])
      newArray.push(arr[value]);
  }
  return newArray;
}

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

You can’t test if a value is equal to NaN in a switch or == or ===. Your solution based on truthiness is the way to go.