Javascript falsy bouncer

Javascript falsy bouncer
0.0 0

#1
function isTrue(value) {
  return value != false && isNaN(value) === false && value !== null;
}

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

bouncer(["a", "b", "c"]);

I can’t figure out why my code is removing strings from the array. From what I can tell, strings are not equal to false, NaN or null.

Any ideas or tips?


#2

I can’t figure out why my code is removing strings from the array.

This is because you set the expression: isNaN(value) === false

// Rememebr: If false, .filter() will drop the element.
console.log(isNaN("string") === false);
// => false

The expression is returning false when true and true when false.
Further read about isNaN().

Perhaps you meant: isNaN(value) !== false.

It looks like a typo or a simple mistake, not a big deal.
Good luck.


#3

You are making a reinvent-the-wheel error that many people do when it comes to the Falsy Bouncer. Here is the last time I explained it:


#4

Ah that totally understand what you’re saying. When I began the problem that’s what I was trying to get at, but couldn’t really put it into code. Thank you.