freeCodeCamp Algorithm Challenge Guide: Falsy Bouncer

freeCodeCamp Algorithm Challenge Guide: Falsy Bouncer
0

#1

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

:checkered_flag: Problem Explanation:

Remove all falsy values from an array.

Relevant Links

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

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

:speech_balloon: Hint: 3

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

try to solve the problem now

Spoiler Alert!

687474703a2f2f7777772e796f75726472756d2e636f6d2f796f75726472756d2f696d616765732f323030372f31302f31302f7265645f7761726e696e675f7369676e5f322e676966.gif

Solution ahead!

:rotating_light: Advanced Code Solution:

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

:rocket: Run Code

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

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

:clipboard: NOTES FOR CONTRIBUTIONS:

  • :warning: 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.
  • Add an explanation of your solution.
  • Categorize the solution in one of the following categories — Basic, Intermediate and Advanced. :traffic_light:
  • Please add your username only if you have added any relevant main contents. (:warning: DO NOT remove any existing usernames)

See :point_right: Wiki Challenge Solution Template for reference.


Falsy Bouncer help me
#2

#3

#4

A less advanced solution:

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

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

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


#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) {
var answer = arr.filter(function(teste) {
return teste;
});
return answer;
}

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


#20

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