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

Game will not acknowledge if the second guess is correct?

Hi everyone Loving the course so far but am pulling my hair out with this part. The game code works all apart from one aspect. If i guess incorrectly first time round, I get the second guess but for some reason if I guess correctly second time around - the game does not acknowledge this and will throw up a sorry you're wrong still.

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 ) { correctGuess = true; } else if (parseInt(guess) < randomNumber) { var guessMore = prompt('Guess again - Higher maybe?'); if (parseInt (guessMore === randomNumber)) {correctGuess = true};

} else if (parseInt(guess) > randomNumber) { var guessAgain = prompt('Guess again - Lower maybe?'); if (parseInt (guessAgain === randomNumber)) {correctGuess = true};

} if ( correctGuess ) { document.write('<p>You guessed the number!</p>'); } else { document.write('<p>Sorry. The number was ' + randomNumber + '.</p>'); }

Any help would be appeciated! Thanks

3 Answers

You should change if (parseInt (guessMore === randomNumber)) and if (parseInt (guessAgain === randomNumber)) to if (parseInt(guessMore) === randomNumber) and if (parseInt (guessAgain) === randomNumber)

Your post is hard to read, maybe you could use Markdown formatting later.

Thank you! Much appreciated! Sorry about the confusing nature, I'm new. How do you use markdown formatting?

Cheers Andy

No worries. Check the Markdown Cheatsheet under the answer input field.

Thanks!