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 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,110 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
Henrik Christensen
Python Web Development Techdegree Student 38,322 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??