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 trial2 Answers
Steven Parker
231,268 PointsYou're quite right that this job can be done with only one "if", and good eye for spotting it!
But the purpose of the lecture is to show how boolean variables can be used, and the code shown gives a clear example. As a "best practice" developer, you'll employ your skills to create code that's both clear and efficient, but in the courses you can expect the teachers to occasionally forgo efficiency when illustrating a particular principle.
Doron Geyer
Full Stack JavaScript Techdegree Student 13,897 PointsHi , quite new to programming ( my first programming ever doing this course) could you provide an example of how you would reduce this to a single if statement?
Cheers, Doron
Doron Geyer
Full Stack JavaScript Techdegree Student 13,897 Pointsvar correctGuess = false;
var randomNumber = Math.floor(Math.random() * 6 ) + 1;
console.log(randomNumber);
var guess = prompt('I am thinking of a number between 1 and 6. What is it?');
if (parseInt(guess) === randomNumber ) {
correctGuess = true;
document.write("you guessed the correct number");
} else{
document.write("your guess was incorrect")
}
I assume like this?
Steven Parker
231,268 PointsYes, good job. And now you don't need "correctGuess" anymore and can omit it from the code.
Kishan P
9,921 PointsKishan P
9,921 PointsThanks man!