Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

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
215,939 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
215,939 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!