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 trialSam Weeks
Front End Web Development Techdegree Student 16,699 PointsHow 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>')
}
2 Answers
Steven Parker
231,236 PointsYou 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) { <-/
Sam Weeks
Front End Web Development Techdegree Student 16,699 PointsYea i don't understand why its doing that thanks for taking a look though :)
Steven Parker
231,236 PointsIt's because currently it tests the 2nd number even if the first one was right.
Sam Weeks
Front End Web Development Techdegree Student 16,699 Pointsi 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
231,236 PointsTry 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
Front End Web Development Techdegree Student 16,699 PointsMthe 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
Front End Web Development Techdegree Student 16,699 PointsYea I just donβt know when to move on to the next lesson I think Iβm tripping myself up sometimes. Thanks though Steven :)
Sam Weeks
Front End Web Development Techdegree Student 16,699 PointsSam Weeks
Front End Web Development Techdegree Student 16,699 Pointssteve i think i cracked it mate