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 trialJohn Silverstein
6,290 PointsWhy 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?
Steven Parker
231,271 PointsNo, an assignment is never a shortcut for a comparison!
Rick Beltran
889 PointsIts 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
231,271 PointsThis 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.
Gil Silva
3,220 PointsBecause 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
6,471 PointsIf (CorrectGuess) is the same as... If (CorrectGuess === true)
If (!CorrectGuess) is the same as... If (CorrectGuess === flase)
Seth Roope
6,471 PointsThanks Steven, edited to prevent confusion per your comment.
Steven Parker
231,271 PointsA 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
6,290 PointsI 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
231,271 PointsThat 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.
John Silverstein
6,290 PointsThanks, much obliged
Gil Silva
3,220 PointsBecause 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.
Rick Beltran
889 PointsRick Beltran
889 Pointsits a shortcut instead of comparing nested conditions.