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

Sam Weeks
seal-mask
.a{fill-rule:evenodd;}techdegree
Sam Weeks
Front End Web Development Techdegree Student 16,699 Points

How does this look?

So in addition to the number guessing game giving you one attempt i thought i would try and figure out how to manipulate the browser to give you two attempts instead... It worked so i'm learning :) however does this code look ok to experienced programmers

cheers

sam

var correctGuess = false;
var correctGuess_2 = 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 ) {
  correctGuess = true;
}
if (correctGuess===true) {
  document.write('<p> You guessed the number!</p>')
}  if(correctGuess===false){
alert("That was incorrect try again")
var guess2 = prompt("Second guess attempt");

} if(parseInt(guess2)===randomNumber) {
correctGuess_2 = true;
} if (correctGuess_2 === true) {
  document.write('<p>You got it that time!</p>');
}
 else {
  document.write('<p>Sorry. The number was '  + randomNumber + '</p>')
}
Sam Weeks
seal-mask
.a{fill-rule:evenodd;}techdegree
Sam Weeks
Front End Web Development Techdegree Student 16,699 Points

steve i think i cracked it mate

var correctGuess = false;
var correctGuess_2 = 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 ) {
  correctGuess = true;
}
if (correctGuess===true) {
  document.write('<p> You guessed the number!</p>')
} else {
alert("That was incorrect try again")
var guess2 = prompt("Second guess attempt");

} if(parseInt(guess2)===randomNumber) {
correctGuess_2 = true;
} if (correctGuess_2) {
  document.write('<p>You got it that time!</p>');
}
 else if (correctGuess!== true&&correctGuess_2 !== true){
  document.write('<p>Sorry. The number was '  + randomNumber + '</p>')
}

2 Answers

Steven Parker
Steven Parker
229,732 Points

You might want to adjust the logic a bit to change what it does when you guess correctly the first time:

You guessed the number!
Β 
Sorry. The number was 3

And you never need to compare a boolean value to "true":

//if (correctGuess === true) {  <-- not needed
  if (correctGuess) {          // this does the same thing

Also you can condense code by eliminating unneded variables and control structures like this:

if (parseInt(guess2) === randomNumber) {
//  correctGuess_2 = true;        <-\
//}                               <-- these 3 lines are not necessary and can be deleted
//if (correctGuess_2 === true) {  <-/
Steven Parker
Steven Parker
229,732 Points

It's because currently it tests the 2nd number even if the first one was right.

Sam Weeks
seal-mask
.a{fill-rule:evenodd;}techdegree
Sam Weeks
Front End Web Development Techdegree Student 16,699 Points

i tried all this created more problems lol i'll have to get back to it later im slowly understanding it once i think i'm really getting it i hit a wave of constant obsticles.

Steven Parker
Steven Parker
229,732 Points

Try sticking to the lessons for a while, don't worry about how an "expert" would do things just yet.

Making things work is far more important that optimizing the code. When you find that easy to do, then you can lean to be more efficient.

Sam Weeks
seal-mask
.a{fill-rule:evenodd;}techdegree
Sam Weeks
Front End Web Development Techdegree Student 16,699 Points

Mthe other half of your comments came through for some reason. My internet must of been slow I’ll take another look. Thanks again Steven :)

Sam Weeks
seal-mask
.a{fill-rule:evenodd;}techdegree
Sam Weeks
Front End Web Development Techdegree Student 16,699 Points

Yea I just don’t know when to move on to the next lesson I think I’m tripping myself up sometimes. Thanks though Steven :)