# Basic Algorithm Scripting - Falsy Bouncer

I don’t understand why my function isn’t working. I see the function putting each value of the array into Boolean(). Then it uses a ternary operator. If Boolean() returns true nothing happens, if it returns false, the array is spliced that element is removed.

``````  **Your code so far**
``````
``````function bouncer(arr) {
for (let i of arr) {
Boolean(arr[i]) ? arr : 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/103.0.0.0 Safari/537.36`

Challenge: Basic Algorithm Scripting - Falsy Bouncer

This is not the proper use case for a ternary. Regardless, because you are mutating the array while looping, `i` may not be the index you are hoping to remove.

First, a ternary is not a general purpose replacement for an if statement. This is a bad place to put a ternary.

Second, mutation of an array as you iterate over it can lead to buggy and surprising results, because you are reindexing the array as you loop.

Also, you might want to `console.log(i)` to make sure you understand what you are referencing with `arr[i]`.

I see thank you! Is there a good rule of thumb for when I should use a ternary and when not to?

Got it! Thank you, that makes sense.

A ternary is for returning a value based upon a condition.

An if statement is for executing logic based upon a condition.

I see, thx! I solved the problem btw! This was my solution. However, when I used a for of loop I would get empty braces instead of the answer. I thought for of iterated through each element of the array.

``````function bouncer(arr) {
let result = [];
for (let i = 0; i < arr.length; i++) {
if (Boolean(arr[i]) === true) {
result.push(arr[i]);
}
}
return result;
}
``````

It does, but you were mutating the array during the iteration over it, causing unexpected results.

Ah I see, thank you!