freeCodeCamp Challenge Guide: Falsy Bouncer

Falsy Bouncer


Problem Explanation

Remove all falsy values from an array.

Relevant Links


Hints

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.

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…

Hint 3

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


Solutions

Solution 1 (Click to Show/Hide)
function bouncer(arr) {
  const filteredArr = [];
  for (let i = 0; i < arr.length; i++) {
    if (arr[i]) filteredArr.push(arr[i]);
  }
  return filteredArr;
}

Code Explanation

  • We create a new empty array (filteredArr).
  • We use a for cycle to iterate over all elements of the provided array (arr).
  • We use the if statement to check if the current element is truthy or falsy.
  • If the element is truthy, we push it to the new array (newArray). This result in the new array (filteredArr) containing only truthy elements.
  • We return the new array (filteredArr).

Relevant Links

Solution 2 (Click to Show/Hide)
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.

Relevant Links

116 Likes

A less advanced solution:

function bouncer(arr) {

   function truthy(value) {
   return value;
   }

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

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

please could you explain me this code?
thanks

6 Likes

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

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);
    }
  });
}
14 Likes

I am talking about @HarinaPana solutions

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]);
11 Likes
function bouncer(arr) {
// Don't show a false ID to this bouncer.
    return arr.filter(function(val) {
        return !!val ;
});
}
8 Likes

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;}
15 Likes

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 :slight_smile:

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

16 Likes

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

4 Likes

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) {
  return arr.filter(badValues);  
}

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

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]);
2 Likes

bit different but here is mine :slight_smile:

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]);

2 Likes

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.

1 Like

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;}
});
}

1 Like

My code is:

function bouncer(arr) {
var answer = arr.filter(function(teste) {
return teste;
});
return answer;
}

But I don’t understand why _function(teste)_and myFunction(teste) doesn’t work.

Hi can you explain this code. How does it evaluate ‘el’ to true?