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 Basics (Retired) Making Decisions with Conditional Statements Improving the Random Number Guessing Game

Seokhyun Wie
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
Seokhyun Wie
Full Stack JavaScript Techdegree Graduate 21,594 Points

Regarding the last 'if' clause

Hi Guys,

if (correctGuess) {
  document.write('<p>You guessed the number!<p>');
} else {
  document.write('<p>Sorry. The number was ' + randomNumber + ".</p>");
}

Why there's no true or false statement in the 'if' clause and just 'correctGuess' is placed?

var correctGuess = false;

this is the initial statement. And I think there should be clear declaration regarding correctGuess, but the program is working perfectly..why? Thanks.

3 Answers

Jamie Reardon
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
Jamie Reardon
Treehouse Project Reviewer

Hi Brandon, the assumption that correctGuess is equal to true is because of the default behaviour of the if statement.

// This is the same as using correctGuess == true inside the condition.
if ( correctGuess ) {
  // ...
}

// We can also override the default behaviour of the if statement by using the not (!) operator like so:
if ( !correctGuess ) { // Means if not true
  // ...
}

// Another way of doing this
if ( correctGuess !== true ) {
  // ...
}

Because correctGuess = true, so: correctGuess = 1, therefore it exist.

if( 1 ) { /*do something } I know it's weird. I guess we just have to accept this way of thinking. It's like a shortcut.

There is an assumption that it's true. It looks funny but this is correct. Your way is correct as well.

Seokhyun Wie
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
Seokhyun Wie
Full Stack JavaScript Techdegree Graduate 21,594 Points

Hi Piotr, So do you mean that by typing only the variable ”correctGuess”, you make an assumption that it is true? Can you explain where is the assumption? Thanks.