Table of Contents

Falsy bouncer? Just in case your mind's been pacing frantically trying to make sense of the title of this challenge, worry not! We’ll do that together.

A bouncer is a person employed by a nightclub or similar establishment to prevent troublemakers and other unwanted from people entering or to eject them from the premises.

Yup!** That big scary guy.**

Now you’re wondering what this has to do with JavaScript. Well, in this challenge, we’ll be strengthening our array manipulation skills by writing a bouncing function that removes every falsy element from within an array.

What is a falsy value?

A falsy value is a value that is considered false when examined as a Boolean. Recall that a Boolean value could either be true or false. JavaScript uses type conversion to translate any value to true or false when required. Examples of falsy values are: false, null, undefined, 0, NaN, and "".

Isn’t this cool? We’get to create our own big scary guy. Having understood the context of this challenge, let’s dive right in.

You should already have your development environment setup for this course. Open the cloned repository and inside the Beginner folder, navigate to the falsyBouncer folder. Our work for this challenge will be done in here. Make sure to follow along in the index-START.js file.

In this approach we use a for…of loop to access every element(value) within the array and then carry out a check to see if it is falsy.

function falsyBouncer(array) {
let result =[]
//loop through with each array value
for (value of array){
// push into result if truthy
if(value){
result.push(value)
}
}
return result
}

Remember that the condition in an if statement evaluates to true or false. Thus, using an if statement, we check if the value evaluates to true or false. If it evaluates to true, we push it into our final result array which is initially empty. If it evaluates to false however, the block of code doesn't get executed and we move on to the next value.

At the end of the iteration, result now contains all the truthy(non-falsy) values, and it is returned as such.

We have now successfully created our own bouncer algorithm. Our performance test has also revealed that a using a for…of loop is a more optimal solution than using the .filter() method. However, both do not vary by much performance-wise.