I’m a bit puzzled as to why the first two tests pass but not the others - I know it could be solved with filter but I originally thought of it this way, even if it’s definitely not pretty:
function bouncer(arr) {
let newArr = [];
for (let i = 0; i < arr.length; i++) {
let item = arr[i];
if (item !== false && item !== null && item !== 0 && item !== undefined && item !== NaN && item !== "") {
newArr.push(item);
}
}
return newArr;
}
bouncer([7, "ate", "", false, 9]);
Try logging newArr to the console before returning it. You’ll notice on those tests your function returns NaN. This is because NaN !== false evaluates to true and NaN !== NaN evaluates to true. Welcome to JavaScript NaN
NaN compares unequal (via == , != , === , and !== ) to any other value – including to another NaN value. Use Number.isNaN() or isNaN() to most clearly determine whether a value is NaN. Or perform a self-comparison: NaN, and only NaN, will compare unequal to itself.
I’ll point out though that if (NaN) will evaluate to false.