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

Jonathan Ramirez
Jonathan Ramirez
6,284 Points

Script not running

Hi guys,

I need help debugging this. I just need an extra pair of eyes to spot where my code went wrong. I'm not even getting the first dialogue box. (Oddly, when I delete everything including the first "else if" and after, then the first dialogue box runs just fine.)

Is it possibly because I have indentation wrong?

Jonathan Ramirez
Jonathan Ramirez
6,284 Points
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 is higher than " + guess);
  if (parseInt(guessMore) === randomNumber) {
        correctGuess = true;
  }
} else if (parseInt(guess) > randomNumber) {
  var guessLess = prompt("Try again. The number 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>");
}

3 Answers

Devron Baldwin
Devron Baldwin
3,508 Points

Generally using a decent IDE will help with this sort of thing. The markup syntax highlighting tends to show you where the code has gone a bit sideways.

I use Netbeans (http://www.netbeans.org) but you can use more lightweight IDEs like Sublime Text (http://www.sublimetext.com).

Also it's worth checking out the console logs on your web browser. They will generally tell you what the issue is or at least point you in the general direction.

Devron Baldwin
Devron Baldwin
3,508 Points

Hi Jonathan,

You are missing an opening bracket on this line

 if parseInt(guessLess) === randomNumber) {

It should be

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

Also the final line has an error.

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

You need to close a string with the same kind of quotes as you opened it. In this case you opened with a " and so you must close the string with a ". As below:

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

I hope that helps!

Jonathan Ramirez
Jonathan Ramirez
6,284 Points

ah that's embarrassing! Thanks so much! I was going crazy trying to find the error. Lesson learned!