Falsy Bouncer Remove all falsy values from an array

Tell us what’s happening:

Your code so far

function bouncer(arr) {
  // Don't show a false ID to this bouncer.
  return arr.filter(arrValue => arrValue != false && arrValue != "" && arrValue != undefined && arrValue != null && arrValue != 0);
}

bouncer([false, null, 0, NaN, undefined, ""]);


Output Comes As
[null]
Can't Understand Help anyone...

Your browser information:

Your Browser User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/66.0.3359.181 Safari/537.36.

Link to the challenge:
https://www.freecodecamp.org/challenges/falsy-bouncer

Edit: Check with console.log each condition.

You want to return a Boolean.

1 Like

Shortest way:

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

just a question, what does x=>x mean ?

The filter() method creates a new array with all elements that pass the test implemented by the provided function.
so in this case x=>x is same as an if statement.

//Any thing that is not 0, false, null, undefined, empty string, or NaN, will pass the test.

const value = 123;
if(value) {
  console.log(true);
} else {
  console.log(false);
}

Thanks a lot, I wondered if filter(x=>x) is similar to filter(Boolean) ?

That means select all which are true.

2 Likes

My Solution

function bouncer(arr) {

return arr.filter(x=> x>0 | typeof(x)=="string" &&x!="");
}

I thought it was a short answer but reading the post. I have learnt that always is gonna be another shortest way…

function bouncer(arr) {
return arr.filter(x=>x)
}

Based on your code, you won’t get the result of [null]. It should be [NaN].

You didn’t put in the check of NaN in your code. But even if you did the way above, it won’t work either. The reason is that NaN is not equal to NaN, at any point. So you can’t use == or === for this challenge.

What you end up doing now is just list all falsy value literally, see here

As callback function of filter method is a boolean, you could simply convert the value to a boolean. Either one works:

function bouncer(arr) {
  return arr.filter(value => Boolean(value));
}
function bouncer(arr) {
  return arr.filter(value => value);
}

Or simply

function bouncer(arr) {
  return arr.filter(Boolean)
}

Note that “Boolean” is a constructor that takes in one parameter, so the first parameter in the callback function (i.e. the value) is passed into “Boolean” constructor. Hence you get the same result.

1 Like

IMHO it is. As filter expect the first parameter (i.e. the callback function) to return true or false anyway.

filter(x => x) introduces the implicit type conversion (a.k.a coercion), while filter(Boolean) is explicit type conversion.

Those two exclamation points are unnecessary. Also, please blur out full solutions or better yet do not post full solutions to the challenges at all. Stick with hints and examples.

1 Like

Fixed. Thanks a lot :+1:

Thanks, the short and simple answers are the best.

OMG…you make it look so simple.
Thanks

Hi, guys i have the same problem here, why the code below doesn’t work?

function bouncer(arr) {
// Don’t show a false ID to this bouncer.
var falsy = [false, null, 0, NaN, undefined, “”];
for (let i = 0; i<arr.length;i++){
if (falsy.indexOf(arr[i]) >= 0){
arr.splice(i,1);
}
}
return arr;
}

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

it’s because indexOf uses strict equality (===) to find the element but unforturnately that doesn’t work with NaN.
If you can believe it NaN === NaN is false in the JavaScript world. To do an explicit test for NaN you have to use Number.isNaN(). But on the other hand the if-condition implicitly tests whatever expression is in it for truthyness and that makes things much easier.