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

My code is different, but it seems to work. Am I overlooking something that will glitch in some situation I don't get?

var correctGuess = false;
var randomNumber = Math.floor(Math.random() * 6) + 1;
var guess = prompt("pick a number 1-6");
if (parseInt(guess) === randomNumber) {
  correctGuess = true;
}
if (correctGuess) {
  document.getElementById("print").innerHTML = "That's right!";

} else if (parseInt(guess) < randomNumber) {
  var guessMore = prompt("GuessHigher");
    if (parseInt(guessMore) === randomNumber) {
      document.getElementById("print").innerHTML = "That's right!";
    } else {
      document.getElementById("print").innerHTML = "Sorry, the number was " + randomNumber;
    } 

} else if (parseInt(guess) > randomNumber) {
  var guessLess = prompt("Guess Lower");
    if (parseInt(guessLess) === randomNumber) {
      document.getElementById("print").innerHTML = "That's right!";
    } else {
      document.getElementById("print").innerHTML = "You're not even trying, the number was " + randomNumber;
    }

}  

1 Answer

Graeme Oxley
Graeme Oxley
8,931 Points

I don't see it causing a problem. It is simply more code than was necessary.

All of the conditions are clearly defined, and have resolutions to each outcome.

Thanks Graeme, getting code down to it's simplest form is ONE of the things I struggle with. I'm going to take a stab in the dark that you have coding experience prior to coming to treehouse?

Ok, now I feel silly. I took a look at your portfolio. Very nice.