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 Boolean Values

Kishan P
Kishan P
9,921 Points

i am very confused?

why are we using two if statement?

2 Answers

Steven Parker
Steven Parker
231,236 Points

You're quite right that this job can be done with only one "if", and good eye for spotting it! :+1:

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.

Kishan P
Kishan P
9,921 Points

Thanks man!

Doron Geyer
seal-mask
.a{fill-rule:evenodd;}techdegree
Doron Geyer
Full Stack JavaScript Techdegree Student 13,897 Points

Steven Parker

Hi , 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
seal-mask
.a{fill-rule:evenodd;}techdegree
Doron Geyer
Full Stack JavaScript Techdegree Student 13,897 Points

var 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
Steven Parker
231,236 Points

Yes, good job. :+1: And now you don't need "correctGuess" anymore and can omit it from the code.