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 Improving the Random Number Guessing Game

I run this code through the console and it works. Anything wrong with this approach?

var correctGuess = false; var randomNumber = Math.floor(Math.random()*6)+1; var guess = prompt("Guess the number between 1 - 6");

if (parseInt(guess)===randomNumber) {correctGuess=true;} else if (parseInt(guess)<randomNumber) {prompt ("guess more");}

else if (parseInt(guess)>randomNumber) {prompt ("guess less");}

if (correctGuess) {document.write ("<p>Hurray</p>");} else {document.write ("<p>Not Hurray</p>");}

1 Answer

Steven Parker
Steven Parker
228,991 Points

You're good through the first try, then it looks like the user is getting a second chance to guess but they aren't.

For the second guess to work correctly, you'll need to save the result of calling "prompt" back into the "guess" variable. Then, you'll need to test it one more time to see if it matches the "randomNumber".

Very interesting - I think I forgot that javascript console executes the code line after line - never parallelly

Steven Parker
Steven Parker
228,991 Points

It's never "parallel", but it's also not always sequential. You'll see more on that when you get to "event handling".

Happy coding!