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.
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.
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;
}
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.
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.
It is great that you solved the challenge, but instead of posting your full working solution, it is best to stay focused on answering the original poster’s question(s) and help guide them with hints and suggestions to solve their own issues with the challenge.
We are trying to cut back on the number of spoiler solutions found on the forum and instead focus on helping other campers with their questions and definitely not posting full working solutions.
anyway, I think your solution is missing the point of the challenge, which is use the falsiness of values, instead of checking against a list of values