Trying this 2 ways but none are working

Hi, do I need to initialize what a falsey variable is or is arr == true enough to know what it has to be?

I tried two different approaches so far but none are working:

1st try

var newArr = []; // initialize to store result
  for (let i = 0; i < arr.length; i++) { // loop through arr
    if (arr[i] == true) { // if its not falsey
      newArr.push([i]); //push to newArr
  return newArr;

second try

function bouncer(arr) {
return arr.filter([arr == true])  // hint says to use filter and this is what I got from looking at Mozilla docs

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

Your browser information:

User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/88.0.4324.146 Safari/537.36.

Challenge: Falsy Bouncer

Link to the challenge:

You are on the right track here - there are just some minor issues.

In this one, arr[i] == true is your problem. You can’t check for weak equivalence to true. You need to just use arr[i] as if it was a boolean value.

For this one, your callback function for .filter() is not a valid callback function. Your function should be of the form

(arguments) => { /* function body */ }


function callback(arguments){ /* function body */ }

And, as above, you can’t check for weak equivalence to true.

Hmm I’m gonna focus on solving with the first one. I’d have though replacing that line with this could work.
if (arr[i].filter(true))

Can you maybe send me a link to where I can read more on weak vs strong?

**Update ah I got it!

You can always convert any value to a Boolean by manually converting it (or “casting” it) to a Boolean. For a variable value, you can covert it to a Boolean with either:


I’d recommend you get your first solution working using this method as I think that is conceptually the easiest way to understand the solution (it’s also what the hint in the instructions recommends). Then after you have it working you can refactor.

Also, you have a bug in the line



@JeremyLT @bbsmooth solved it with the following! Btw I’ve seen sometimes you guys prefer to hide the solution in the forum. If there is a way I’d be happy to do it on my end here

function bouncer(arr) {
 var newArr = []; // initialize to store result
  for (let i = 0; i < arr.length; i++) { // loop through arr
    if (arr[i]) { // if its not falsey
      newArr.push(arr[i]); //push to newArr
  return newArr;

console.log(bouncer([null, NaN, 1, 2, undefined]));
1 Like

Perfect! The line:

if (arr[i]) {

is exactly the same as

if (Boolean(arr[i])) {

The condition in an if statement is always converted to a boolean, so the if is just doing the Boolean() for you.

You can add [spoiler] and [/spoiler] tags to blur the answer :slight_smile:

@am93, now that you have the correct answer, a good challenge would be to explain to us why

if (arr[i] == true)

doesn’t work. Sometimes knowing why something doesn’t work is just as important as knowing why something did.


I see, so boolean == true means if it’s actually true, but we are checking if it’s not a falsey boolean which is different, correct?

Just confusing though because the output is usually true or false. Maybe I didn’t understand the question or hopefully in the future it will be more obvious to me.

Thanks for pointing this out. I do think it’s good to know why something is correct and why that is significant (ie ways it can get you in the future). Kinda like telling someone to do something is more memorable once they know what can happen if they don’t.

Let’s look at one of the sample arrays used by the test:

[7, "ate", "", false, 9]

The first element, 7, is that a falsey value? No, it is not (I’m trusting you understand why). So when your original if statement evaluates the expression using 7 as the value for arr[i] it should evaluate to true and be pushed to newArray. Now open up your JS console in dev tools and type in the following:

7 == true

I think you can see why even though 7 is not a falsey value it was not being added to newArr. But if you do:


You’ll get the answer you want and the 7 will be pushed to newArr.

Take a look at the MDN docs on loose equality. It explains what happens when you use == and a nice table showing all the possible conversions. If you look closely you should see why 7==true wasn’t coming out the way you wanted it to.

On a side note, if you don’t want to have to remember all those conversions listed in the table (I know I don’t) then simply use === (strict equality) whenever you are checking if two things are equal. I’m not saying there isn’t a place for == from time to time, but you should understand that table before you use it.

1 Like

Sorry. I don’t actually understand what you said and the doc you linked is too confusing for me.

Can you describe without assuming I understand anything other than the fact that falsey boolean = 0, -0, null, false, NaN, undefined, or the empty string ("")? So basically I know why it’s not falsey.

I read this explanation. If you think this past answer is not enough then we can continue discussing it If (arr[i] == true) vs. if (arr[i])

This describes it as if(arr[i] == true) means “if the value of arr[i] is literally the boolean true …”

if(arr[i]) means “if the value at arr[i] is something that is not ‘falsy’…”

Assuming because the problem asked to return those that are not falsy is why we can’t use if(arr[i] == true)

So that’s not actually correct (it would be if it was using strict equality). That’s why I linked you to that table of conversions. When you use weak equality JS does some conversions behind the scenes first before it compares the values on each side of the ==. So let’s take the example of using 7 again for arr[i]:

7 == true

7 is a Number type and true is a Boolean type. If you look at the conversion table then you will see that when comparing these two types using weak equality the Boolean will be converted to a Number first and then the comparison will be made. So true gets converted to a Number, which happens to be the number 1. So now we have:


And obviously that is not true which is why your original code wasn’t working.

I know it can be a little intimidating at first reading the docs and trying to figure this out but I highly recommend that you take the time to do it and don’t be satisfied until you feel you completely understand what is happening with loose equality. And as I mentioned, you should always use strict equality (===) whenever possible to avoid those nasty behind the scenes type conversions that JS so loves to make.

1 Like

Gotcha - thanks. I’ll stick with === until needed otherwise

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