Falsy Bouncer help me

Falsy Bouncer help me
0

#1

Tell us what’s happening:

Your code so far

function bouncer(arr) {
  // Don't show a false ID to this bouncer.
  var i=0;
  var a=arr.length;
  while(i<a-1){
    if(arr[i]==false) arr.splice(i,1);
    if(arr[i]===null) arr.splice(i,1);
    if(arr[i]==0) arr.splice(i,1);
    if(arr[i]=="") arr.splice(i,1);
    
    if(arr[i]==undefined) arr.splice(i,1);
    //if(isNaN(arr[i])) arr.splice(i,1);
    //if(typeof(arr[i]) !== 'number') arr.splice(i,1);
    i++;
  }
  return arr;
}

bouncer([false, null, 0, NaN, undefined, ""]);

Your browser information:

Your Browser User Agent is: Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) coc_coc_browser/66.4.120 Chrome/60.4.3112.120 Safari/537.36.

Link to the challenge:


#2

Get a hint


#3

function bouncer(arr) {

function truthy(value) {
return value;
}

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

In this program can somebody please tell me how does the function truthy gets its arguments because we are not giving one as values are going into arr not value.


#4

filter() is an array method that comes with all Javascript arrays. As designed the parameter of filter is a callback function you provide to test each item in the array. In this case truthy function.

filter will run the callback function on each element in the array. Any item that returns true will be placed in a new array. In this case filtered

The callback will be passed three parameters. You don’t do this - it is done for you. You’re only using the first one in this case- the current element being tested.

.filter() and all other array methods are shown in detail on the MDN

You could write the test function as anonymous function so what you are doing is clearer.

var filtered = arr.filter(function(elem){
  return elem;  // if true - goes in resulting array, if false it does not
});

return filtered;
}

#5

thank you so much for the explaination


#6

Hi Everyone!

I came up with my own solution to this because whilst I understood that just passing in the Javascript Boolean object to the .filter method would work, it didn’t really help me understand why it worked.

It took a while but I came up with the solution below.

I have also included my logic behind each part of the function in the comments as it took me a while to understand what a predicate function is and what exactly filter does (thanks debugger;). I hope this helps anyone else that is struggling with the logic behind this particular challenge.

function bouncer(arr) {
// Don’t show a false ID to this bouncer.

return arr.filter(function returnBoolean (value){
return value;
});
}
bouncer([7, “ate”, “”, false, 9]);

/*
.filter loops through each value.

The callback function (returnBoolean) merely states the value of the array object for each index position as .filter iterates through the array.

In the .filter method a callback function is automatically a predicate function, which means that it will only reduce values to a true or false (boolean) value.

By default, the filter function keeps values that pass the test (true) and discards the ones that don’t (false). Therefore, falsy values are discarded by default.

*/