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

Sean Flanagan
Sean Flanagan
33,235 Points

Why doesn't my program prompt you to do anything?

Hi.

I'm at a loss to understand why my program doesn't work. It doesn't prompt a guess. Here's my HTML:

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <link rel="stylesheet" href="css/main.css">
  <title>Random Number Guessing Game</title>
 </head>
<body>
  <div class="container">
  <h1>Random Number Guessing Game</h1>
   <script src="script.js">

   </script> 
  </div>
</body>
</html>

And my JavaScript:

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'd be grateful for any help.

Sean

3 Answers

You took the arrow and all caps I added to direct you to the error out, right? Just making sure! I should have commented it but I did it too quickly.

Sean Flanagan
Sean Flanagan
33,235 Points

Hi Amy. I hadn't, but I have now and it now works fine. Thanks for your help; it was really nice of you.

I've upvoted both your posts and given you a best answer for the second.

Sean :-)

Ayush Bahuguna
Ayush Bahuguna
2,092 Points

Hi Mama, My JavaScript is

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) < random number){
  var guessMore = prompt('The number I am thinking of is greater than ' + guess);
  if (parseInt(guessMore) === randomNumber) {
    correctGuess = true;
  }
} else if ( parseInt(guess) > random number) {
  var guesLess = prompt('The number I am thinking of is smaller 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>');
} 

But, the prompt guess is not coming up.

Christopher Mlalazi
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
Christopher Mlalazi
Front End Web Development Techdegree Graduate 17,305 Points

Wooo! I had the same curly bracket missing and I just couldn't see it until I decided to sift carefully through discussion, and you came to the rescue Amy! Thanks a lot.

The second else if statement is missing a closing bracket. When the conditional statement isn't closed, you get an Unexpected End of Statement error. I added some extra indentation and highlighted where the bracket should go. Once I added this, the prompt came up like it should.

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;
      }
} <---HERE IS THE BRACKET THAT WAS MISSING

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

Hi Amy. I copied and pasted your JavaScript into my workspace. When I previewed it though, I got no prompt. I don't know what's happened.

Thanks

Sean :-)

Thank you! It's the first time I've been able to answer a question for someone! I'm glad it helped.

Thank you helping for helping your fellow student out!