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/ Safari/537.36

Challenge: Basic Algorithm Scripting - Falsy Bouncer

Link to the challenge:

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.

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) {
  return result;

Ah I see, thank you!

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