Basic Algorithm Scripting: Falsy Bouncer - Help needed

Hi everyone,

I am currently stuck with the following project. I don’t understand why my code does not work. I’d be happy about a longer explanation since I am a beginner.

Please try to explain it using my code. Thank you very much!

Basic Algorithm Scripting: Falsy Bouncer
Remove all falsy values from an array.
Falsy values in JavaScript are false, null, 0, “”, undefined, and NaN.
Hint: Try converting each value to a Boolean.

My approach/ code:

function bouncer(arr) {

  let myVar = 0;

  for (let i = 0; i < arr.length; i++) {
    myVar = arr[i];
    if (!myVar) {
      arr.splice(i, 1);


  return arr;

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

Your issue is the splice method, splice change the array on which you are iterating, as such the length change and the same arr[i] can refers to different elements between one iteration and an other

Use to see visually what happens step by step (use the JavaScript Tutor version of it)

If you want to use splice, you may want to start iterating at the end of the array

1 Like

Thank you very much! That helped a lot!

Just wondering, do you suggest any other way instead of using splice?

You can use filter(), or push() (as in, creating an array and pushing to it the truthy things and returning it at the end)