Can anyone tell why my code isn't working

Tell us what’s happening:
Describe your issue in detail here.
my code isn’t looping through all elements in arr i don’t know why.

  **Your code so far**

function bouncer(arr) {
let falsyValues = [false, "null", 0, "", undefined, "NaN"];
for(let i in arr){
  if(falsyValues.indexOf(arr[i]) >= 0){
    //delete arr[i];
    arr.splice(i,1);
    continue;
  }
}
return arr;
}

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

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

Challenge: Falsy Bouncer

Link to the challenge:

First thing first, this is a truthy value as it is a string with content inside, it is not falsy. "null" is a string, null is the falsy value you need to remove

same for NaN

but an other reason fro which this will not work is that NaN === NaN is always false. And it is reason why you need to use the falsyness of the values to solve this in a simple way.

1 Like

because splice changes the array, removing items - the loop is following the indexes, but you change what’s at each index (add console.log(arr) inside the loop to see this).
Do not change arrays you are iterating over, it gets complicated.

1 Like

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