Falsy Bouncer in Basic Algorithm Scripting

I am doing an exercise called, Falsy Bouncer in Basic Algorithm Scripting.
Here is my code:

function bouncer(arr) {
  let res = [];
  for(let i = 0; i < arr.length; i++){
    if(arr[i] !== false && arr[i] !== null && arr[i] !== 0 && arr[i] !== "" && arr[i] !== undefined && arr[i] !== NaN){
      res.push(arr[[i]]);
    }
  }
  return res;
}

Error:

// running tests
bouncer([false, null, 0, NaN, undefined, ""]) should return [].
bouncer([null, NaN, 1, 2, undefined]) should return [1, 2].
// tests completed

I think my code is enough to fix this issue. But in reality, it’s not.
Please help me to figure out what’s going on?

You cannot evaluate NaN == NaN and get true. The equivalence for NaN just never works like that.

But, really you don’t want to. The big idea behind this exercise is that ‘truthy’ values are values that are treated like a logical condition that evaluates to true when used in place of a logical conditional, like in the head of an if statement.

1 Like

Helpful Article

1 Like

this is also not the correct syntax to access an array element, you have an extra couple of brackets

1 Like

Thank you :grimacing:

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.