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 trialSeokhyun Wie
Full Stack JavaScript Techdegree Graduate 21,606 PointsRegarding 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
Treehouse Project ReviewerHi 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
Front End Web Development Techdegree Graduate 29,324 PointsBecause 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
Full Stack JavaScript Techdegree Graduate 21,606 PointsThanks again for your time :) I am already kinda used to accepting designated knowledge while learning this course, so will try. Thanks
Piotr Manczak
Front End Web Development Techdegree Graduate 29,324 PointsThere is an assumption that it's true. It looks funny but this is correct. Your way is correct as well.
Seokhyun Wie
Full Stack JavaScript Techdegree Graduate 21,606 PointsHi 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.
Seokhyun Wie
Full Stack JavaScript Techdegree Graduate 21,606 PointsSeokhyun Wie
Full Stack JavaScript Techdegree Graduate 21,606 PointsThanks Jamie! It was helpful ππ»