Can't get past the Falsy Bouncer algorithm

I’m getting off by one on only the last test case. All other cases work out, but for some reason, it’s removing the wrong elements from the Array. It should leave behind [1, 2] but my output is [NaN, 1]. My assumption is that I’m skipping an indices, but I can’t see how to fix it.

  **My code so far**

function bouncer(arr) {
let indexArr = [];
for(let i = 0; i < arr.length; i++) {
  if(!Boolean(arr[i])) {
    console.log('Falsy found at index ' + i);
    indexArr.push(arr[i]);
  }
}
for(let i = 0; i < indexArr.length; i++) {
  console.log('Removing element "' + indexArr[i] + '"');
  arr.splice(arr.indexOf(indexArr[i]), 1);
}
console.log('New Array:');
console.log(arr);
return arr;
}

bouncer([null, NaN, 1, 2, undefined]);
  **Your browser information:**

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

Challenge: Falsy Bouncer

Link to the challenge:

Taking a look at the indices which are being found could confirm some suspicions:

const index = arr.indexOf(indexArr[i]);
console.log(index);

Thank you, I did find that the indices being removed are 0, -1, and -1. So, my indexOf() function is returning -1 twice. As far as my understanding goes, it only returns -1 when the element doesn’t exist in the Array, but in this case I’m splicing specific values and using those to pass through the indexOf() function. Why would my it be returning -1 here, but didn’t in other test cases?

When I change all the Falsy values to null, it works. Does the indexOf() funciton not work with the values NaN and undefined?

Well… indexOf() checks for equality. Should NaN === NaN?

let first = 1/0; // NaN
let second = -3/0; // also NaN
console.log(first === second);

Okay, I didn’t know that. Thank you. I had to create an if statement with a for loop with an if statement to get it to work without rewriting the whole thing. No my code looks like this and it works! Thanks guys

function bouncer(arr) {
  let indexArr = [];
  for(let i = 0; i < arr.length; i++) {
    if(!Boolean(arr[i])) {
      console.log('Falsy found at index ' + i);
      indexArr.push(arr[i]);
    }
  }

  console.log('Removing Falsies');
  
  for(let i = 0; i < indexArr.length; i++) {
    if(arr.indexOf(indexArr[i]) >= 0) {
      arr.splice(arr.indexOf(indexArr[i]), 1);
    } else {
      for(let i = 0; i < arr.length; i++) {
        if(Number.isNaN(arr[i])) {
          arr.splice(i, 1);
        }
      }
    }
  }
  console.log('New Array:');
  console.log(arr);
  return arr;
}

bouncer([7, "ate", "", NaN, false, 9]);
type or paste code here

Why all those loops? Why not just use the logic here

And instead of keeping a bunch of indices of falsy things instead keep the actual truthy values?

Good question haha. Because I didn’t think to just create a new array. I was stuck thinking I had to manipulate the old one. Which is silly because I’ve handled other problems like that. Thank you for pointing that out

1 Like