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

Jennifer Ciaccio
seal-mask
.a{fill-rule:evenodd;}techdegree
Jennifer Ciaccio
UX Design Techdegree Student 11,137 Points

Why do we we need to define correctGuess?

If a boolean is inherently true or false, why do we have to define correctGuess ?Can't we just say if (guess === true)?

4 Answers

Steven Parker
Steven Parker
231,268 Points

The variable "guess" is not a boolean, and holds the answer given by the user (a string), so it could not be used that way.

But, you do have the right idea in that "correctGuess" could be eliminated by using only the first "if" (that compares the input to the random number) to control the outputs directly.

I suspect the extra step is being used to illustrate the boolean nature of the comparison being made, but that could also be done more directly with an assignment (and no conditional):

var correctGuess = parseInt(guess) == randomNumber;

Just remember that the code examples you see are provided to illustrate the concepts being introduced, and will not necessarily be examples of the most efficient way to accomplish a specific task.

Sam Gord
Sam Gord
14,084 Points

well, i think we need to compare guess with the Correct guess to find out if its really true or not! if we just say

if(guess === true){}

it means that if there is actually a guess (aside from it's being true or not) but we don't know if its really the true answer or not because we don't have any meter for that. by defining correctGuess we actually have our meter to check the correct of not correct answer .

happy coding ;)

Jennifer Ciaccio
seal-mask
.a{fill-rule:evenodd;}techdegree
Jennifer Ciaccio
UX Design Techdegree Student 11,137 Points

Thanks guys, that makes sense. I think it was confusing to me by using a variable called "correctGuess" I was running on the assumption it took a user input first. It's also very early here in California and I may not be fully awake yet. Thanks for the quick responses!

Steven Parker
Steven Parker
231,268 Points

Jennifer Ciaccio — Glad to help. You can mark a question solved by choosing a "best answer".

Happy coding!

Siddharth Pande
Siddharth Pande
9,046 Points
JavaScript
do {
var correctGuess = false;
var randomNumber = Math.floor(Math.random() * 6 ) + 1;
var guess = prompt('I am thinking of a number between 1 and 6. What is it?');
if (parseInt(guess) === randomNumber ) {
  document.write('<p>You guessed the number!</p>');
  correctGuess = true;
} else {
    alert('Sorry. The number was ' + randomNumber+'.');
  var playAgain = prompt('Do you want to try your luck again? Enter "Y" to play again or "N" to stop.' );
  if (playAgain.toUpperCase() === "N") {
    correctGuess = true;
  }
}
}
while (!correctGuess);```