Query about Falsy Bouncer

Tell us what’s happening:
Hi, I am trying to splice null elements from the array arr but the code below is still returning the original array. Can you please let me know where I am going wrong?

Regards

Ritwik

  **Your code so far**

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

return arr;
}

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

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/93.0.4577.82 Safari/537.36

Challenge: Falsy Bouncer

Link to the challenge:

It’s not the original one
image

But it’s not the right one.

Do not use splice to change a loop you are iterating over, it will disalign the loop and cause unexpected results

2 Likes

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