I succeeded this challenge but could someone explain me why this solution cannot work?

Tell us what’s happening:
I succeeded the challenge, but I have a strong wondering about the first way I tried to resolve the challenge. Can someone explain me why the code below cannot work? Theoretically it should no? I also tried with a switch but logically the result is the same than if condition.

I did some tests, for example with the “null” value, it can remove the “undefined” from the array, but not the “null”. This is just a thing I realised.

  **Your code so far**

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

console.log(bouncer([7, "ate", "", false, 9]));
console.log(bouncer([false, null, 0, NaN, undefined, ""]));
console.log(bouncer([null, NaN, 1, 2, undefined]));
  **Your browser information:**

User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/98.0.4758.87 Safari/537.36

Challenge: Falsy Bouncer

Link to the challenge:

The issue is that you are manipulating the array that you are also checking. It is not about undefined or null - it is about the position and what happens to the array relative to the index.

When in doubt, put in log statements. Try this code:

function bouncer(arr) {
  for (let i = 0 ; i < arr.length ; i++) {
    console.log('arr before check', arr)
    console.log('checking index', i, '   element', arr[i])
    if (arr[i] === false || arr[i] === null || arr[i] === 0 || arr[i] === "" || arr[i] === undefined || arr[i] === NaN) {
      arr.splice(i, 1);
    }
    console.log('arr after check', arr)
  }
  return arr;
}

console.log(bouncer([null, NaN, 1, 2, undefined]));

Pay attention to where the the index is and how that relates to what is being checked. See if you can figure out how they are changing and how that breaks the solution.

If you can’t figure it out, let us know and I’ll try a better hint.

What you have is fixable, but it gets a little hinky, imho.

I should also point out that the logic for your if statement can be greatly simplified. Do you understand the concept of “falsy” and how if statements handle them?

1 Like

You also have an issue with how you are testing NaN. You should read up on that here. Or change it to the simpler method at which I hinted.

1 Like

Hello @kevinSmith,

Thank you for your answer. I completely figured out what’s happen in that solution with your suggestions. In fact, my solution will check each index. The issue is that I am deleting parts of the array and the indexes evolve, but the loop continue the logic of 0, 1, 2 etc. Then it will not check anymore the previous indexes. An element who was index 1, can become index 0, and then will be forget into the loop.

Right. You could fix that by decreasing the index when needed. You also need to fix the NaN problem above. Here is a simplified version:

    if (!arr[i]) {
      arr.splice(i--, 1);
    }

Remember that the postfix decrement operator operates after the variable is evaluated. For clarity, you could also do it on the next line, instead of inline.

Personally, I don’t like this solution - I don’t like messing with an array length while I’m looping it. And I think it’s hinky to mess with the interation variable for a for loop. Even if you code it perfectly, there is no guarantee that some other coder is not going to come back later, misunderstand it, and add a bug.

But it’s useful to understand this approach, and its strengths and weaknesses.

Incidentally, you could probably also fix your approach, without doing the decrement, if you loop in the other direction.

I agree with you, and I gave up this idea. I preferred used a simpler solution than this one and less messy. What is good is that I understood that I need to be more careful with my loops and conditions approach. It looks like I have some shortcomings.

We all have shortcomings, even the best coders. We just keep working and get better.

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