# Basic Algorithm: False bouncer

Hello fellow learners!

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, ""]);
``````

returns `[false, null, 0, NaN, ""]`

``````function bouncer(arr) {
var filtered = arr.filter(function (value){
if(value !== undefined){
return value;
}
})
console.log(filtered);
}

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

returns `[]`

ThanX !

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.

1 Like

Of course !!! “if” works as a truth checker!

``````if (something true){
return whatever you say
}
``````

Whereas in the first one, I was returning what was undefined!
(⁄ ⁄•⁄ω⁄•⁄ ⁄) What an idiot I am!

Thanks for taking the time to answer, @lionel-rowe ! Much appreciated.

No problem!

Don’t put yourself down, even the best programmers need an extra pair of eyes on their code from time to time.

1 Like