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

Seokhyun Wie
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
Seokhyun Wie
Full Stack JavaScript Techdegree Graduate 21,606 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 ) {
  // ...
}
Piotr Manczak
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
Piotr Manczak
Front End Web Development Techdegree Graduate 29,365 Points

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.

Seokhyun Wie
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
Seokhyun Wie
Full Stack JavaScript Techdegree Graduate 21,606 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.