Falsy Bouncer: what's wrong?

Hello,

https://www.freecodecamp.org/learn/javascript-algorithms-and-data-structures/basic-algorithm-scripting/falsy-bouncer

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

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

What’s wrong in my code ?

You are changing the array as you loop over the array. This causes trouble because you keep changing the indices of all of the elements. Also, it is a best practice to never change your input arrays unless it is explicit in the description of the function that you intend to do so.

You’re incrementing to the full length of arr…but also shortening arr. Think woody woodpecker sitting on a limb whilesawing it off

2 Likes

Yes I just undertood that. I’m looking for.

The issue is, you’re changing the array while you’re working on it. Instead, consider making a second array, and those things you want to keep from the first one? Add them to that second. The ones you want to lose, simply don’t add.

1 Like

I given up with “splice”.
My code works now:

function bouncer(arr) {
  let newArr = [];
    for (let i = 0; i < arr.length; i++) {
    if (Boolean(arr[i]) === true) {
    newArr.push(arr[i]);
    }
  }
  console.log(newArr)
  return newArr;
}

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

I don’t know if you have a solution with “splice” ?

snowmonkey:
I think my solution enswer to your post but without ‘splice’.

1 Like

It is possible to make a solution with splice, but a) it is against best practices and b) it is not a very ‘clean’ approach.

1 Like

ok thank you at all :wink:

Another one but not as short as the freecodecamp solution:

arr = arr.filter(item => Boolean(item) === true);
  return arr

Why you didn’t add a button for “spoiler” tag ?

Doing the push solution, at this point, is the best option. Well done!

If you click on the gear, you’ll see more options…including the spoiler.

Again, this changes the input, which is generally discouraged. In this case, it does not change the input outside of the function, but it can still be confusing to read.

Yes Jerermy, roger :wink:

arr = arr.filter(item => Boolean(item) === true);
console.log(arr);
return arr

Thanks snowmonkey

Another one, just for ending :slightly_smiling_face:

function bouncer(arr) {
  let newArr = [];
    for (let elem of arr) {
      if (elem) {
        newArr.push(elem);
      }
    }
  console.log(newArr)
  return newArr;
}

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

‘for of’ is even shorter.
Wich is better between ‘for of’ and ‘for’ loops ?
I would tend to use ‘for of’ because it 's shorter :thinking:

For…of is slightly more declarative, in that you can see the connection: for this of that, as opposed to an arbitrary loop to some variable.

If you can find ways to write your code so it describes itself? Generally the better approach.

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.