Falsy bouncer confusion

Hey everyone! I’m trying to complete the falsy bouncer algorithm, but I can’t seem to get it quite right.

My main problem is that it filters out only “false” when I need it to filter out all falsy values.

Got any sugestions?

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

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

Best,
Cy499_Studios

This is a strict comparison operator, so you will only filter out values that are literally, exactly false.

I would instead use the idea of truthiness and falsyness.

if (true) console.log("'true' is truthy");
if (5) console.log("5 is truthy");
if ("") console.log("/"/" is truthy");
if (null) console.log("null is truthy");

Hi @Cy499_Studios !

It might also be helpful to review the docs on if statements.

If statements only execute if the condition is truthy.

You basically want your code to say, if the current element is truthy then push it to the array.

Hope that helps!

1 Like

!== false is a bit like using double negatives. What is the opposite of “not false” and would checking for that be more appropriate?

1 Like

Thanks @JeremyLT !

So I changed the argument to arr[i] != false but two of the last tests are still failing and I’m not sure why, also, @lasjorg I’m checking for everything that’s not false. Also @jwilkins.oboe what counts as truthy?

You are still making a comparison against false. Why not use the inherent truthiness or falsyness of values? You don’t need to make a comparison against true or false inside of your condition.

What is another word for this?

@lasjorg
@JeremyLT
I suppose the answer would be true but I know that I can’t just put true so I’m still confused however I know none of you are about to give me the Answer (I mean this as a good thing, after all, a calm sea never made a good sailor) so I suppose more research is needed. If anyone else has any other tips I can use I’ll be apreciative.

Do you see a comparison against true or false in any of these examples? What happens when you run this code?

@JeremyLT

I know I’m being closed-minded but I’ll at least guess.

What I think the code is doing is that it’s basically checking for a value to then confirm the value in the log.

Is that correct?

What output do you get when you run it?

Or this

if (true) console.log("aaaaaa");
if (5) console.log("bbbbbbbb");
if ("") console.log("ccccccc");
if (null) console.log("dddddddddd");
1 Like

Do you mean run the code in your example?

Yep. If you actually run those 4 lines, what happens?

1 Like

OK I ran all and only the first two ran because they were truthy values but how do I put that into a for loop?

Well, what do you need to check for truthiness or falsyness?

1 Like

@JeremyLT
I think I cracked the case

Because there’s no room left for falsy variables falsy is automatically filtered out

@JeremyLT

All tests passed, thank you for your help! :grinning_face_with_smiling_eyes:

4 Likes

Right but at least now you are checking for truthy without looking for the opposite negated.

Next comes what JeremyLT is talking about. The if statement will coerce the expression to a Boolean value. If you pass it a value it will turn it into a Boolean.

You can also use the Boolean() function yourself, it isn’t needed but still handy to know about. It can also be more explicit about what is happening.

From the challenge:

Hint: Try converting each value to a Boolean.

console.log(Boolean(false))// false
console.log(Boolean(null))// false
console.log(Boolean(""))// false