Hi All,
I just recently completed the challenge Falsy Bouncer and passed it, but I’m not sure I quite understand how this code is working to give me the correct answer lol.
function bouncer(arr) {
var falsy = [false, null, 0, "", undefined, NaN];
arr = arr.filter(function(val){
for ( var i = 0; i < falsy.length; i++ ){
if ( val !== falsy[i] ) {
return val;
}
}
});
return arr;
}
bouncer([7, "ate", "", false, 9]);
Please take a look at my JavaScript Tutor https://goo.gl/csK09D
The for loop goes through and looks at the value falsy(0) which is 7 which then goes down to the if statement which is true because of course 7 does not equal false, and then it returns the value of 7.
Here is where I don’t understand how or why this works. This code I created doesn’t iterate over the whole array of falsy… ever. So how could it possibly know if the val !== equal the values that are within the falsy array?
Did I just somehow create a statement to trick the system to work, or is JavaScript Tutor not showing some aspect?
I’m thinking an indexOf method might be a little better for this, but if someone could please explain I’d really appreciate because I want to make sure I’m understanding this 100%.