I have a task to remove false, null, 0, "", undefined, and NaN elements from an given array. I worked on a solution which removes all except null. Anyone can explain why? Here's the code:
function bouncer(arr) {
var notAllowed = ["",false,null,0,undefined,NaN];
for (i = 0; i < arr.length; i++){
for (j=0; j<notAllowed.length;j++) {
arr = arr.filter(function(val) {
return val !== notAllowed[j];
});
}
}
return arr;
}
bouncer([1,"", null, NaN, 2, undefined,4,5,6]);