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

Hunter Shaw
Hunter Shaw
2,187 Points

What's wrong with my code? I don't understand how it doesn't even run.

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("Try again. The number I'm thinking of is more than " + guess);

if ( parseInt(guessMore) === randomNumber) {

correctGuess = true;

}

} else if ( (parseInt(guess) > randomNumber) {

var guessLess = prompt("Try again. The number I'm thinking of is less than " + guess);

if (parseInt(guessLess) === randomNumber) {

correctGuess = true;

}

}

if ( correctGuess ) {

document.write('<p>You guessed the number!</p>');

} else {

document.write('<p>Sorry. The number was ' + randomNumber + '.</p>');

}

// I'm pretty sure it has something to do with my brackets on my "if else" statements.

3 Answers

Hunter Shaw
Hunter Shaw
2,187 Points

I figured it out, turns out I had an extra parenthesis on my (both)

knowing that one parenthesis can mess up your whole program is crazy to me.

I've been trying to figure this out for a while and just noticed it.

-- and debugging begins. :) jaja

} else if ( (parseInt(guess) > randomNumber) {

Matthew Long
Matthew Long
28,407 Points

Glad you figured it out! It is very easy for a single character to break your entire program. This is why organization is so important!

For future reference wrap your code with 3 backticks (```) on the line before and after. If you specify the language after the first set of backticks, that'll help with syntax highlighting.

 ```html
 <p>This is code!</p>
 ```
Chris Sundström
Chris Sundström
4,875 Points

Hunter, let me give you some tips.

  • Instead of creating two new var's, I would recommend you to reuse your 'guess'-var.
  • Indent your code, for better readability.
  • Maybe to much new lines. Keep the code more compact.
  • If the user answer correctly on the first guess, non of the other code need to run. I use an else-statement.

But, I like that you´re leaving breathing room in you code, between the parentheses and equalsigns, and so on.

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 ) { 
        guess = prompt("Try again. The number I'm thinking of is more than " + guess);
    } else {
        guess = prompt("Try again. The number I'm thinking of is less than " + guess);
    }
    if (parseInt(guess) === randomNumber) {
        correctGuess = true;
    }
}

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

Keep up the coding!! You´re doing great!! // Chris

Hunter Shaw
Hunter Shaw
2,187 Points

The fact that this website doesn't recognize that it's code is a HUGE reason why it's not working in the first place.