DEV Community

Viren B
Viren B

Posted on • Originally published at

Solving "Falsy Bouncer" / freeCodeCamp Algorithm Challenges

Solution can also be found on my blog,

'Falsy Bouncer' Challenge

Let's solve freeCodeCamp's basic algorithm scripting challenge, Falsy Bouncer.

Starter Code

function bouncer(arr) {
  return arr;

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


Remove all falsy values from an array.

Falsy values in JavaScript are false, null, 0, "", undefined, and NaN.

Hint: Try converting each value to a Boolean.


bouncer([7, "ate", "", false, 9]) should return [7, "ate", 9].
bouncer(["a", "b", "c"]) should return ["a", "b", "c"].
bouncer([false, null, 0, NaN, undefined, ""]) should return [].
bouncer([null, NaN, 1, 2, undefined]) should return [1, 2].

Our Approach

Read everything first. Read the instructions clearly, read the starter code we're given, and read the tests and understand what has to be returned.

  • The function takes in one argument. The argument, arr, is an Array.
  • arr holds all different data types in each test (strings, numbers, booleans, null, undefined).
  • fCC gives a hint, to try to convert each value to a Boolean.
  • We want to return an array without any falsy values.

Now that we understand what we are given and what we want to output, let's see how we can work with arr.

Initially, it seems like we will have to go through each index in the array, and evaluate if the item is a truthy or falsy value.

We know there are a few ways to traverse through the array now so we will come back to that.

Let's explore the hint fCC provided, using Boolean.

MDN Documentation: Boolean

"The Boolean object is an object wrapper for a boolean value."

We can use this to check on each item. It will return us a true or false value.

So if we traverse through arr, wrapping each item in arr with Boolean, it will return true or false.

We are getting there but we want:

  • To return the actual item's values, not a true or false
  • Remove the item's which are not truthy

If we use map() to traverse through arr, it helps but it doesn't return what we're looking for:

function bouncer([7, "ate", "", false, 9]) {
  return => Boolean(elem));
Ouput: [ true, true, false, false, true ]

Using a for loop would provide the same results.

Instead of map(), there is another array method, filter(). Just by the name, it should maybe provide what we want.

MDN Documentation: Array.prototype.filter()

"The filter() method creates a new array with all elements that pass the test implemented by the provided function."

So, it shall take care of the two points listed above. It will return the values, not true or false. It will also remove non truthy values as the documentation mentions it 'pass the test'. The Boolean will be the test.


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

Links & Resources

'Falsy Bouncer' Challenge on fCC


Donate to FCC!

Solution on my GitHub

Thank you for reading!

Top comments (1)

namhle profile image
Nam Hoang Le

Or you just need:

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