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

Natalia Karolinskaya
Natalia Karolinskaya
9,367 Points

Why do we assign 'false' to correctGuess?

I mean we could assign 'true' from the start?

2 Answers

Julian Gutierrez
Julian Gutierrez
19,201 Points

In this program you are checking to see if the user's inputted guess is correct. correctGuess shouldn't initially be true because then any number the user inputs would evaluate to a "correct answer."

Jason Anders
Jason Anders
Treehouse Moderator 145,858 Points

That's correct, and I misread the question. :) Too early in the morning for me, I guess.

John Murphy
John Murphy
1,122 Points

I am not sure what it is you're working on, but generally where a boolean such as "correctGuess" comes up its to validate some input to determine the next sequence of events in the program.

If we were to initialize the value of "correctGuess" to "true", it would assume that any input is "correct" and follow that sequence of events - which is obviously not good. I have an example of this being used in a simple Hangman Java app that I did for a college project that you can see below if you like.

In my case, it was used to determine whether or not the users guessed letter was a match. If so, correctGuess = true - which then updated the statistics and proceed to the next task.

https://github.com/woodyeire/College-Java-Project---Hangman/commit/112ffd6781ca372aaad605bc4b0fad08d0cc0061