Falsy Bouncer done with a for loop passes half the tests - why?

Got stuck on this basic algorithm:


I’m a bit puzzled as to why the first two tests pass but not the others - I know it could be solved with filter but I originally thought of it this way, even if it’s definitely not pretty:

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

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

what’s going on here? :disappointed_relieved:

Try logging newArr to the console before returning it. You’ll notice on those tests your function returns NaN. This is because NaN !== false evaluates to true and NaN !== NaN evaluates to true. Welcome to JavaScript NaN :wink:

NaN compares unequal (via == , != , === , and !== ) to any other value – including to another NaN value. Use Number.isNaN() or isNaN() to most clearly determine whether a value is NaN. Or perform a self-comparison: NaN, and only NaN, will compare unequal to itself.

I’ll point out though that if (NaN) will evaluate to false.

1 Like

If you’re looking for true / false., try using this function:

const booleanValue = (value) => Boolean(value);

For truthy/falsy values (the name of the challege is Falsy Bouncer) it’s as simple as x => x (this function is usually called id or identity)