What's wrong with the code. why it isn't accepting the false value in the if statement?

Tell us what’s happening:
Describe your issue in detail here.

  **Your code so far**

let a = false;
if (!!a === false) console.log(8);

> here the if condition returns true

 function bouncer(arr) {
for(let i = 0; i < arr.length; i++){
  if(!!arr[i] === false) {

But here it doesn't

arr.splice(i, 1);
  }
}
return arr;
}

console.log(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/98.0.4758.102 Safari/537.36

Challenge: Falsy Bouncer

Link to the challenge:

Splicing changes the size of the array and changes the indexes of all later elements. Changing the array as you iterate over it is generally a bad idea.

the problem is with the if() statement why it isn’t returning true when we pass false in it

No. The problem is not with the if statement.

The problem is that you should not remove items from an array as you iterate over it.

Suppose that you start with the array arr=[1,0,0].

In your algorithm, we start with i=0. arr[0] is truthy, so we continue and increment i.

Now i=2. arr[1] is falsey so you cut it out. You have arr=[1,0] and increment i to 2.

Now i=2 and arr.length=2, so we’re all done with your algorithm.

Mutating an array as you iterate over it is bad. Don’t do it.

Generally it’s a bad idea to mutate function inputs anyways.

1 Like

So I have to store the values into a new array …
by the way thanks for the help…

Good idea, a new array is exactly how I would do it. Then you don’t have this mismatch between i and the array.

1 Like

Appreciate the help man… :pray:

1 Like