Falsy Bouncer Remove all falsy values from an array

Falsy Bouncer Remove all falsy values from an array
0

#1

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:


#2

Edit: Check with console.log each condition.

You want to return a Boolean.


#3

Shortest way:

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

#4

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


#5

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);
}

#6

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


#7

That means select all which are true.


#8

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)
}

#9

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.


#10

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.


#11

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.


#13

Fixed. Thanks a lot :+1:


#14

Thanks, the short and simple answers are the best.


#15

OMG…you make it look so simple.
Thanks