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 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
215,984 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,884 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,884 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
215,984 Points

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