Basic Algorithm Scripting - Falsy Bouncer using Splice()

Tell us what’s happening:
Describe your issue in detail here.
Hi Everyone

Just wondering why this solution isn’t working (seems to me it should). I’m iterating the array, and I use the !! operator to check if the item at arr[i] is falsy or not. If it is falsy, I’m calling splice() to remove that falsy value.

I’ve checked separately both my use of the !! operator and the splice() method, and they both seem to be working independently. So why aren’t they working when combined? Frustrating.

Thanks for your help!

  **Your code so far**
function bouncer(arr) {

for (let i = 0; i < arr.length; i++) {
  if (!!(arr[i])) { // if item at i location is false
    arr.splice(i,1); // at the i location, remove one value (that is, remove the item at the i location)
  }
}

console.log(arr);
return arr;
}

bouncer([7, "ate", "", false, 9]);
  **Your browser information:**

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:102.0) Gecko/20100101 Firefox/102.0

Challenge: Basic Algorithm Scripting - Falsy Bouncer

Link to the challenge:

add console.log({i, arr, "arr.length": arr.length, "arr[i]": arr[i], "i < arr.length": i < arr.length}) inside your loop, and look at what’s logged, you may discover something interesting

Thanks Ilenia for the quick reply first of all! Really appreciated. I can see what you’re hinting at.

I’ve actually managed to find the two problems, in the meantime, reading an earlier post by someone. My first mistake was using !!arr[i] (which evaluates to true) instead of !arr[i] that evaluates to false.

My second mistake was that after using splice(), the array itself changes, so i (incremented by the for loop) effectively skips some array elements. I have to decrement i manually whenever I remove a falsy value. So the corrected and working code looks like this:

function bouncer(arr) {

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

Maybe just don’t use splice, it’s bad pracrice to change the input of the function

1 Like

If you are changing the increment variable inside of the loop, that’s a sign that you should not be using a for lop with that loop body. Either your loop body or your loop type needs to change.

1 Like

Thanks @ilenia and @JeremyLT , you’re both right. It’s a force of habit to just go for a for loop. And I just went for splice() because I still can’t remember that there is another, similarly-named method called slice() that does the same thing as splice(), without changing the input array.

Had I just used slice(), both problems would have been avoided. Alternatively, I saw in an official solution, that splice() can be used in a while loop, and that just takes care of the whole problem! I guess I made a mishmash of the slice() solution and the splice() solution. I really hope it’s one of these things that experience will minimize or even clear.

Thanks for your comments, I’ll keep them in mind.

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