Solving "Falsy Bouncer" / freeCodeCamp Algorithm Challenges 
Tags: freecodecamp, algorithms, challenge, javascript
Published: 2020-06-17 15:12:16

---
title: Solving "Falsy Bouncer" / freeCodeCamp Algorithm Challenges 
published: true
description: My guide, notes, and solution to freeCodeCamp's basic algorithm challenge, "Falsy Bouncer" 
tags: #freeCodeCamp, #algorithms, #challenge, #javascript
canonical_url: https://virenb.cc/fcc-013-falsy-bouncer
series: Solving freeCodeCamp's Algorithm Challenges
---

Solution can also be found on my blog, [https://virenb.cc/fcc-013-falsy-bouncer](https://virenb.cc/fcc-013-falsy-bouncer)

!['Falsy Bouncer' Challenge](//images.ctfassets.net/yzwpq1epaq68/2bxHO1Ut9OmIIAOlwRRTtK/6f56241148cbd8c068f1c0930a1b328c/ss-013.png)

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

### Starter Code
```javascript
function bouncer(arr) {
  return arr;
}

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

### Instructions

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.

### Tests
```
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](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Boolean "MDN Docs: 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 arr.map(elem => 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()](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/filter "MDN Docs: 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.

---

# Our Solution [SPOILER: CONTAINS ANSWER]

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

---


### Links & Resources

['Falsy Bouncer' Challenge on fCC](https://www.freecodecamp.org/learn/javascript-algorithms-and-data-structures/basic-algorithm-scripting/falsy-bouncer "Basic Algorithm Scripting: Falsy Bouncer Challenge on fCC Website")

[freeCodeCamp](https://www.freecodecamp.org/, "freeCodeCamp")

[Donate to FCC!](https://www.freecodecamp.org/donate "fCC Donation Page")

[Solution on my GitHub](https://github.com/virenb/fcc-algorithms/blob/master/013---falsy-bouncer.js "GitHub Solution")

Thank you for reading!