# freeCodeCamp Algorithm Challenge Guide: Falsy Bouncer

freeCodeCamp Algorithm Challenge Guide: Falsy Bouncer
0

#1

Remember to use `Read-Search-Ask` if you get stuck. Try to pair program and write your own code

### Problem Explanation:

Remove all falsy values from an array.

## Hint: 1

Falsy is something which evaluates to FALSE. There are only six falsy values in JavaScript: undefined, null, NaN, 0, “” (empty string), and false of course.

try to solve the problem now

## Hint: 2

We need to make sure we have all the falsy values to compare, we can know it, maybe with a function with all the falsy values…

try to solve the problem now

## Hint: 3

Then we need to add a `filter()` with the falsy values function…

try to solve the problem now

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

### Code Explanation:

The `Array.prototype.filter` method expects a function that returns a `Boolean` value which takes a single argument and returns `true` for truthy value or `false` for falsy value. Hence we pass the built-in `Boolean` function.

## Credits:

If you found this page useful, you can give thanks by copying and pasting this on the main chat:

`Thanks @renelis @abhisekp @Rafase282 for your help with Algorithm: Falsy Bouncer`

## NOTES FOR CONTRIBUTIONS:

• DO NOT add solutions that are similar to any existing solutions. If you think it is similar but better, then try to merge (or replace) the existing similar solution.
• Categorize the solution in one of the following categories — Basic, Intermediate and Advanced.

See `Wiki Challenge Solution Template` for reference.

Falsy Bouncer help me
#2

#3

#4

``````function bouncer(arr) {

function truthy(value) {
return value;
}

var filtered = arr.filter(truthy);
return filtered;
}

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

#5

please could you explain me this code?
thanks

#6

@dahider2 which do you need help with, the Advanced solution or @HarinaPana’s solution?

#7

Beginner/Intermediate solution:

``````function bouncer(arr) {
//Only return values that evaluate as true inside the array.
return arr.filter(function(value){
if (value){
return (value);
}
});
}``````

#8

I am talking about @HarinaPana solutions

#9

Hope this helps:

``````function bouncer(arr) {

function truthy(value) {
//only return the given value if it evaluates as true.
//For example, 2, "string", true, will all return, but false, NaN, -1 will not.

return value;
}

var filtered = arr.filter(truthy);
//This is filtering only the values that evaluate as true in the given array (following the truthy function give above)

return filtered;
}

//The function is now run with a given array, returning only the values that evaluate as true

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

#10
``````function bouncer(arr) {
// Don't show a false ID to this bouncer.
return arr.filter(function(val) {
return !!val ;
});
}``````

Javascript ! ! : Explain please
#11

### Super basic solution using forEach and push. Checks all values for true and pushes true ones into a new array.

``````function bouncer(arr) {
var a = [];
arr.forEach(function(el){
if(el){
a.push(el);}});
return a;}``````

#12

I managed it with this

``````function bouncer(arr) {
var newArr= [];
for (var i= 0; i < arr.length; i++){
if (arr[i])
newArr.push(arr[i]);
}
return newArr;
}
``````

I was far, very far from the easy solution given in this topic

there’s no solution, there’s solutionS
Myself – Just now

#13

I came very close…

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

Damn… could have condensed it down a line. Programmers want to get back to our Geekdom pursuits. Less lines = more time gaming

#14

lol I used a bit different approach, here is the code:

``````function badValues(val){
return val !== false && val !== null && val !== 0 && val !== "" && val !== undefined && !Number.isNaN(val);
}

function bouncer(arr) {
}

bouncer([1, null, NaN, 2, undefined]);``````

#15

My solution was a little longer, but it made a lot more sense to me and actually didn’t take long at all.

My code is pasted below, but essentially, I made a loop which would go through each element of the array and put it through a boolean operator to see if it was true. If it’s true, then push that element to a new array called noFalsy. Then, it filters the new noFalsy array for any values of null, because the nature of this particular loop means that if you have items in the middle of your array that are false but perhaps one at the end that is true, all of those false values will become null values in the new array.

``````function bouncer(arr) {

var noFalsy = [];
for (i = 0; i < arr.length; i++) {
var x = Boolean(arr[i]);
if (x === true) {
noFalsy[i] = arr[i];
} else {
}
}
//filters function and removes any items that are null
var noFalsyFinal = noFalsy.filter(function(noFalsy){
return noFalsy !== null;
});

return noFalsyFinal;

}

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

#16

bit different but here is mine

function bouncer(arr) {
// Don’t show a false ID to this bouncer.
var a=true;
var newArr=arr.filter(function(a){
return a;
});
return newArr;
}

bouncer([7, “ate”, “”, false, 9]);

#17

My code looks like this,

``````    function bouncer(arr) {
// Don't show a false ID to this bouncer.
var newArr = arr.filter(function(element){
if(Boolean(element)){
return element;
}
});
return newArr;
}
``````

Boolean(x) means that we take the value and transform it to boolean, as a result get true or false.

#18

Mine was similar but you can cut it down by just returning the filtered array, without needing newArr

function bouncer(arr) {
return arr.filter(function(val){
if (Boolean(val)){
return val;}
});
}

#19

My code is:

function bouncer(arr) {