Falsy Bouncer - Please explain solution

Falsy Bouncer - Please explain solution
0.0 0

#1

Please could someone explain to me how the following code works?

function bouncer(arr) {
  return arr.filter(function(item) {
    return item;
  });
}

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

This passes the Falsy Bouncer challenge, but I don’t understand what part of it is calculating whether or not the ‘item’ is a truthy or falsy if no conditions have been set. I’ve been researching the .filter() method and JS return statements but can’t find an explanation for this.



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

#2

I’ve edited your post for readability. When you enter a code block into the forum, precede it with a line of three backticks and follow it with a line of three backticks to make easier to read. See this post to find the backtick on your keyboard. The “preformatted text” tool in the editor (</>) will also add backticks around text.


#3

Do you understand what “falsy” means? Can you describe it?
Do you understand how filter functions work? Can you describe it?


#4

A falsy value is the equivalent of the boolean expression ‘false’. In the same way that the expression 6 < 7 = false, a falsy value such as (0) inherently = false.

(First time I’ve considered trying to explain this in plain English - I’ll definitely read around this subject to make sure I fully understand it).

The filter method uses a function which sets a condition, and creates a new array from any elements that pass the condition.


#5

:+1:
Here is the missing piece. A filter function returns a value which is evaluated as a boolean. If the return value is truthy, then the currently-inspected item is kept in the array. If the return value is falsy, the currently-inspected value is removed from the array.


#6

Ah, of course! If no conditions are set then it will just go ahead and filter out truthy values. Brilliant, thanks for your help!