Help Understanding Falsey Bouncer

I am trying my own version but i dont know why it does not work. The false value is not removed from the array. Any idea?

function bouncer(arr) {
    arr.forEach((item, index, arr) => {
            if ([false, null, 0, "", undefined, NaN].includes(item)) {
                arr.splice(index, 1);
            }
        }
    );
    return arr;
}

bouncer([7, "ate", "", false, 9]);

Hello there,

I moved your question to its own topic because you were asking a question related to your own code for a challenge and were not answering the OP of the other thread. It is always best to create your own thread for you specific question(s). Also, it is advisable to use the Ask for Help button on the challenge, so it auto-populates with your current code and the challenge url.

Thank you.

Regarding your question, the reason the code does not entirely work is because you are altering an array whilst you are looping through it.

Here is an example:

let arr = [1,2,3,4,5,6,7,8,9];

for (let i = 0; i< arr.length; i++) {
  console.log(arr[i]);
  if (arr[i] % 2 == 0) {
    arr.splice(i,1);
  }
}

Hope this helps

Umm , I have made the same mistake in a couple of more challenges :(. Lessons learned
Thankss

As a side note, you can make this much simpler by using the idea of falsy.

Try this code:

if (0)
  console.log("'0' is truthy");
if (1)
  console.log("'1' is truthy");
if ("")
  console.log("'' is truthy");
if ("a")
  console.log("'a' is truthy");