Boo who confoosion

Boo who confoosion
0
#1

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:
https://learn.freecodecamp.org/javascript-algorithms-and-data-structures/basic-algorithm-scripting/boo-who

0 Likes

#2

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.

0 Likes

#3

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

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

0 Likes

#4

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

0 Likes

#5

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

0 Likes

#6

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.

0 Likes

#7

so "logical or " is invalid?

what about

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

0 Likes

#8

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.

0 Likes

#9

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”

0 Likes

#10

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.

0 Likes

#11

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

?

0 Likes

#12

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.

0 Likes

#13

is that not the goal of the question?

0 Likes

#14

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

0 Likes

#15

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

#16

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

?

0 Likes

#17

if you try it what do the tests say?

2 Likes

#18

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

1 Like