Falsy Bouncer - Basic Algorithm

Trying to remove all falsy values from the array. Here’s my approach to it, and I think I’m close! What am I doing wrong here?


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

console.log(bouncer([7, "ate", "", false, 9]));

HI @NathanVu !

A few things.

No.1:
Remember that this means assignment not equals.

Equals is == and strictly equals is this ===

No.2:
I get what you are trying to say here but this is not the correct way to write this statement.

I get that you are trying to say. if arr[i] equals undefined or null or NaN, etc.

But the correct way to write this would be

if(arr[i] === undefined || arr[i] === null ....

But there is a much simpler way to solve this problem. :grinning:

We want to return an array with only truthy values right?

We could create a variable and assign an empty array to it.
Then by definition of the if statement, if the current element in the array is truthy then we can just push that to a new array and return that new array.

Here is the documentation for if statements

That approach is simpler than trying to write a lengthy if statement for all falsey values and then trying to remove them from the array.

Hope that helps!

1 Like

Hey thank you so much for putting me in the right position. I had a feeling this could be more simplified. Thanks for the correction, I’ll look into the documentation!

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