How to implement is Boolean function in JS?

How to implement is Boolean function in JS?
0

#1

I bump into this challange: https://www.freecodecamp.com/challenges/falsy-bouncer

By reading desc of task, u need to remove “falsy” boolean values. So i tried something like this:

EDIT: At the first moment, i link to challange with code, but think link is to large for showing whole code so here is:

function isFalsy(value){
  return (value !== false) && value !== null && value !== "" && value !== undefined && value !== 0 &&  typeof(value) !== NaN;
}

var haveString = true;

function bouncer(arr) {
  // Don't show a false ID to this bouncer
  console.log(arr);
  arr = arr.filter(isFalsy);
  console.log(arr);
  return arr.length === 0 ? []:arr;
}

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

And can’t figure out how on one side can have function that check if value is NotANumber and remove if so, and on other have String

Something like:

bouncer([7, “ate”, “”, false, 9]) should return [7, “ate”, 9]

bouncer([1, null, NaN, 2, undefined]) should return [1, 2]

But with this code works perfectly:

function isFalsy(value){
  return value;
}

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

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

#2

Your code not only works, it’s the intended answer – except your isFalsy function really needs to be called isTruthy (or just identity) to accurately describe it.

The point of the challenge is to introduce the notion of “truthiness” and “falsiness” that are part of JavaScript, and that’s exactly what your algorithm captures.


#3

I don’t look at challange as if it just introduce, that’s is subjective point of looking, i bump into this issue, i little search on google javascript boolean implementation but can’t find to compare this one and that.