Boo who confoosion

Tell us what’s happening:
hello,
I am wondering my code isnt correct? I actually looked at a hint and, realized the typeof pathway, and solved the question. But I am still confused why my way is incorrect.

thank you!

Your code so far


function booWho(bool) {
 if (bool = "true" | "false") {
 return true
 }
 return false;
}
booWho(null);

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.110 Safari/537.36.

Link to the challenge:

bool = "true" This is an assignment, not a comparison.

| This doesn’t mean anything
"false" This is just a string. It doesn’t do any evaluation.

if (bool == “true” || “false”) {
return true
}

I realize I made syntax mistakes, but is the entire idea nonsense?

That will always be true becaues "false" is truthy.

what if bool == 75

wouldnt that return false?

edit:

perhaps you mean
if (bool == true || false) {
return true
}
return false

since “false” and false are different

No. bool == “true” || “false” only compares bool to “true”. Then, no matter what bool is, it ends up evaluating to true because a non-empty string is truthy. You aren’t comparing “false” to anything.

so "logical or " is invalid?

what about

if (bool == “true”) {
return true
}
else if (bool == “false”) {
return true
}
return false

A logical or is perfectly valid. The or was working, but on one side of it you had a truthy value instead of a comparison.

is this what you mean? perhaps I didnt edit that comment in time before you responded

my best understanding right now is that a string is always truthy, so it must be false where I put “false”

if (bool == true || false) will only be true if the variable bool is equal to the boolean value true. The || false does not add anything because false is false.

if (bool == true || bool == false) {
return true

?

That would be true if the variable bool is equal to either of the boolean values (true or false). It would not be true if bool were 75.

is that not the goal of the question?

I’m just telling you what the code snippet you wrote would do.

in this case it is better you use the scrict equality comparison ===, otherwise it would get true also for something like booWho("") (empty string, it is falsy, so "" == false is true; but if you use the strict equality "" === false this is false)

When you are using boleans, use always the strict equality operator

From the You Don’t Know JavaScript: Up&Going book: (I just started that series of books and it is really interesting)

  • If either value (aka side) in a comparison could be the true or false value, avoid == and use === .
  • If either value in a comparison could be one of these specific values ( 0 , "" , or [] – empty array), avoid == and use === .
  • In all other cases, you’re safe to use == . Not only is it safe, but in many cases it simplifies your code in a way that improves readability.
1 Like

if (bool === true || bool === false) {
return true
}
return false

?

if you try it what do the tests say?

2 Likes

success! thanks for spending an hour of your life on that.

1 Like