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.
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:
Boolean(value)
or !!value
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.
@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]));
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:
Boolean(7)
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.
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.
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:
7==1
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.