Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

JavaScript JavaScript Loops, Arrays and Objects Simplify Repetitive Tasks with Loops Exiting Loops

In the If at the end.Why do we not add correctGuess= true.?

In the If statement in the end why do we not do it like this: if (correctGuess= true) { document.write("<h1> You Guessed the number right.</h1>"); document.write("It took you " + guessCount + " tries to guess the correct number " + randomNumber + "."); } else if (correctGuess= false){ document.write("<h1> Sorry. You did not guess the number.</h1>") }

4 Answers

Patrik Horváth
Patrik Horváth
11,085 Points

Check your code and check IF statment

=   is for Asign value to 
Example : 
var a = 2;

== is for equal check :)

var a == 2      -> this return true or false

so you cant do this

 if (correctGuess= true)
Henrik Christensen
seal-mask
.a{fill-rule:evenodd;}techdegree
Henrik Christensen
Python Web Development Techdegree Student 38,319 Points

Here is another way of doing it:

EDIT: if you just need to check if a variable is true or truthy, then you don't really need to say == true, == false etc..

if (correctGuess) { // same as saying "correctGuess == true"
  // do something
} else if (!correctGuess) { // same as saying "correctGuess != true"
  // do something
} else {
  // do something
}

So if I add:

if (correctGuess === true).

Would that be right.?

Brian Prouty
Brian Prouty
1,792 Points

I think I am confused about the same thing, I am wondering why when you call the if (correctGuess) later in the code why is that not equivalent to correctGuess = false; at the top? I was under the impression that the only way to call 'correctGuess = true' is either typing it out like that or '(! correctGuess)'... can anyone help explain this??