Falsy bouncer - answer gets rid of all falsy values except for false

why does this return false?

  **function bouncer(arr) {

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


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

console.log (bouncer([7, "ate", "", false, null, 9]));






  **Your browser information:**

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

Challenge: Falsy Bouncer

Link to the challenge:

Your issue is not what you think it is.

You are modifying the input as you loop over it. This means that you are cutting out indices, which leads to you skipping array entries.

1 Like

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