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

How does this code look?

Hey guys, would like some feedback on this code, how does it look? Anything I can improve or should avoid doing?

//Default initial state
var correctGuess = false;

//Random generated number.
var generatedRandomNumber = Math.floor(Math.random() * 6) + 1;

//Asks user to give a number (in 'string' form since the 'prompt' command always returns a string.
var userGuess = prompt("I am thinking of a number between 1 and 6. What is it?");

//Conditional statement with a 'string' to 'integer' conversion included.
if (parseInt(userGuess) === generatedRandomNumber) {
  correctGuess = true;
}

//Second conditional statement
if (correctGuess) {
  document.write("<p>You guessed the number!</p>");
}
else {
  document.write("<p>Oh no, you guessed wrong! The correct answer was " + generatedRandomNumber + "</p>");
}

1 Answer

Adam Beer
Adam Beer
11,314 Points

It's pretty good, it works. I transcribed a little bit I think so cleaner but I think that doesn't matter.

//Random generated number.
var generatedRandomNumber = Math.floor(Math.random() * 6) + 1;

//Asks user to give a number (in 'string' form since the 'prompt' command always returns a string.
var userGuess = prompt("I am thinking of a number between 1 and 6. What is it?");

//Conditional statement with a 'string' to 'integer' conversion included.
if (parseInt(userGuess) === generatedRandomNumber) {
    document.write("<p>You guessed the number!</p>");
} else {
    document.write("<p>Oh no, you guessed wrong! The correct answer was " + generatedRandomNumber + "</p>");
}

Thanks Adam, you guys are fast!