I was working on the False bouncer test, and finally found the solution: so this question is not about finding the solution.
However, while working on it, I tried a couple of things and I came upon two similar codes which give two different outcomes: I just don’t get why it’s different. Can anybody explain why, please?
function bouncer(arr) {
var filtered = arr.filter(function (value){
return value !== undefined;
})
console.log(filtered);
}
bouncer([false, null, 0, NaN, undefined, ""]);
Your second filter function always returns a falsy value (see code comments for why):
function bouncer(arr) {
var filtered = arr.filter(function (value){
if(value !== undefined){
return value; // all the `value`s in the array are falsy
}
// else return `undefined` (in JavaScript, `undefined` is the default return
// value when no `return` statement is run). `undefined` is also falsy.
})
console.log(filtered);
}
bouncer([false, null, 0, NaN, undefined, ""]);
Because all the return values are falsy, everything is filtered out.