Falsy Bouncer challenge Problem 1.0

Tell us what’s happening:
NOTE: i don’t want someone to show me the solution, i want to know why it is not working please!

I don’t understand why this code below is not working as expected

Your code so far

function bouncer(arr) {
for (let val in arr) {
  if(arr[val] === false || arr[val] === null || arr[val] === 0 || arr[val] === "" || arr[val] === undefined || arr[val] === NaN ) {
    arr.splice(val, 1);
return arr;

console.log(bouncer([false, null, 0, NaN, undefined, ""]));

Challenge: Falsy Bouncer

Link to the challenge:

You don’t want to check if your value is on some special ‘falsy’ list. This misses the idea of ‘truthy’ and ‘falsy’.

Try this code

if (1)

if (0)

if (NaN)

if ("hello")

But i don’t understand why the code is not working?

For some falsy values, you cannot check against them.

let badOne = Math.sqrt(-1);
let badTwo = undefined + undefined;

should those two values be strictly equal to each other?

This approach won’t work because NaN === NaN is false. And it misses the power of the idea of ‘truthy’ and ‘falsy’.

1 Like

Let me explain you the “Why” part.

function splice modifies the original array.
Therefore. When you iterate your array in loop, you iterate elements indexes:

First iteration. val = 0.

from array [false, null, 0, NaN, undefined, ""] you eliminate false value + modify the array. It becomes: [null, 0, NaN, undefined, ""]

Next iteration val = 1.
So you eliminate element arr[1] which is 0. Array becomes: [null, NaN, undefined, ""]

Next iteration val = 2 so you eliminate undefined. Array becomes: [ null, NaN, '' ]

val=3 and array.length is also 3 which means you finished iterating the array. So this is your final result: [ null, NaN, '' ]

Yes, but also you just can’t check if NaN === NaN, so this approach just won’t work.

1 Like

??? Why is 0 eliminated before null?

So undefined values can’t be checked?

undefined is different than NaN

You can’t check if two NaN values are the same because there are a ton of ways to make a NaN.

For the order, that is the risk of mutating an array as you loop over it. You are essentially saying
Check index 0. If it’s falsy, remove it.
Check index 1. If it’s falsy, remove it.

and so on.

But as soon as you remove an item, the contents all shift down an index, resulting in skiped items.

1 Like

Are the falsy values equivalent to the false value (i.e. undefined === false ?)

Falsy vales are losely equivalent to false (ie val == false). That’s the definition of falsy.

Similarly, truthy values are losely equivalent to true.


In an if statement,

if (condition) {...

actually is evaluated as

if (condition == true) {...
1 Like

No, they are not equivalent. You can test it yourself in the browser console by writing the expression undefined === false. They are “falsy” not the actual value false.

1 Like
function bouncer(arr) {
  for (let i = 0; i < arr.length; i++) {
    if (arr[i]) {
    } else {
      arr.splice(i, 1);
  return arr;

I’ve solved the challenge using this code, but i find it kinda dumb, because of this if (arr[i]) { }

but the following values are considered falsy values : [ null, NaN, undefined ], but are not ==false

They are indeed losely equivalent to false. Try

console.log(NaN == false);
console.log(undefined == false);

== and === do different things

That approach works, but you can also do this by making a copy of the array that only holds truthy values. The solution looks cleaner, and I typically try to avoid directly modifying inputs.

console.log(NaN == false); //returns false
console.log(undefined == false);//returns false

That’s idiotic. Sometimes JS doesn’t follow its own rules very well.

console.log(NaN != true); // returns true
console.log(undefined != true); // returns true
console.log(42 != true); // returns false

NaN and undefined are not losely equivalent to true, which means they should be losely equivalent to false, but :man_shrugging:

:joy: so does that mean that : (NaN != true ) != (NaN == false)?

Yeah, sometimes the symbolic logic doesn’t line up quite right.