Delete method not working on arr[x] == NaN

I can’t get why my code won’t delete NaN in the given arr:

Your code so far


function bouncer(arr) {
 let arrX = [];
 for (let i in arr) {

  (arr[i] === NaN) ? delete arr[i]: '';
  (arr[i] === 0) ? delete arr[i]: '';
  (arr[i] === false) ? delete arr[i]: '';
  (arr[i] === null) ? delete arr[i]: '';
  (arr[i] === '') ? delete arr[i]: '';
  (arr[i] === undefined) ? delete arr[i]: arrX.push(arr[i]);
  //console.log(arrX);

 }
 return (arrX);
}
**Challenge:** Falsy Bouncer

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

Hi there,
I suggest you reading a bit about NaN on MDN.

If you do you will find that

NaN compares unequal (via == , != , === , and !== ) to any other value – including to another NaN

Luckily there are other methods to check if NaN is actually NaN

hint: isNaN

Hope this helps :slight_smile:

2 Likes

Though, I think that it’s worth mentioning that checking if values are equal to falsy values isn’t leveraging the power of falsy. Falsy values are equivalent to false when used as a Boolean.

2 Likes

Could you please be more explicit, I don’t understand

Many thanks, I altered the algorithm to delete NaN before all other operations

delete arr[arr.findIndex(n => Number.isNaN(n))];

since arr.findIndex(n=> Number.isNaN(n)) return the index of the NaN, It worked :man_dancing:t3:. Many thanks

if you use any falsy value where a boolean is expected, then that value is treated in the same way as false

for example:

if (0) {
   // this block does not execute
}
2 Likes

Exactly. Checking if the values are on a partial list of all Falsy values isn’t the point of the exercise. The point of this exercise is that some variables are equivalent to True and some are equivalent to False when you use them in place of a Boolean, such as the comparison inside of an if statement.

1 Like