Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

JavaScript JavaScript Basics (Retired) Making Decisions with Conditional Statements Improving the Random Number Guessing Game

John Silverstein
John Silverstein
6,290 Points

Why do we type "If (CorrectGuess)" and not if "(CorrectGuess = true)" at the end of all the conditions?

In the final if else block - where we check if there was a correct guess - I tried to set the condition as follows: "if (correctGuess = true)" rather than "if (correctGuess)" - but this way the program always outputs a correct guess message, even when there wasnt a correct guess. why is that?

its a shortcut instead of comparing nested conditions.

Steven Parker
Steven Parker
231,268 Points

No, an assignment is never a shortcut for a comparison!

Its a shortcut, type in true instead of going an comparing conditions. Making the program more simpler. We can agree to disagree. Lets ask the Tutors here and they can decide :)

Steven Parker
Steven Parker
231,268 Points

This is not a matter of opinion. Replacing a condition with true breaks the purpose of the program, and is sersiously incorrect. If you were working as a programmer and took that kind of "shortcut" deliberately on the job, you would likely get fired.

Because the variable has already been declared in the beginning of the statement. First you declare the var CorrectGuess = true. Then you include your variable in the statement CorrectGuess, which has been defined previously.

5 Answers

Seth Roope
Seth Roope
6,471 Points

If (CorrectGuess) is the same as... If (CorrectGuess === true)

If (!CorrectGuess) is the same as... If (CorrectGuess === flase)

Seth Roope
Seth Roope
6,471 Points

Thanks Steven, edited to prevent confusion per your comment.

Steven Parker
Steven Parker
231,268 Points

A single equal-sign is an assignment operator.

This causes CorrectGuess to be true no matter what value it had before, so the condition will always be true.

For future reference, the equality comparison operator is made of two equal-signs ("=="). But you will never need to use it to compare anything with true. Because anything that might be equal to true can stand by itself as the conditional expression.

John Silverstein
John Silverstein
6,290 Points

I see. how about when its inside an if clause, inside the conditional statement. we do write : if (parseInt(**) === **) { correctGuess = true; } why does this expression doesnt cause correctGuess to always be true - is it not an assignment operator in this case?

Steven Parker
Steven Parker
231,268 Points

That assignment is controlled by the if expression.

In this case the assignment will only happen when the condition in the if is true. In your previous example, you were performing the assignment instead of evaluating a comparison. So it would always happen.

Also, your expression appears to have a syntax error. The symbols "**" are an exponentiation operator that requires two operands, and by itself it is not a valid argument to pass to parseInt or something you can compare to.

Because the variable has already been declared in the beginning of the statement. First you declare the var CorrectGuess = true. Then you include your variable in the statement CorrectGuess, which has been defined previously.