JavaScript JavaScript Basics Making Decisions with Conditional Statements Improving the Random Number Guessing Game

Manda Schalin
Manda Schalin
1,193 Points

'Unexpected end of input' console error msg. I've closed the 'else' statement with a curly brace; unsure of the problem

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

Moderator edited: Markdown added so that code renders properly in the forums.

1 Answer

I Dilate
I Dilate
3,634 Points

Hi Manda,

Is there anything else in your script?

The code you pasted looks syntactically fine, so perhaps the problem lies elsewhere?

Thanks, Rich

Manda Schalin
Manda Schalin
1,193 Points

Hi Rich, Thanks a lot for checking! The console pinpoints the error to line 23 (Uncaught syntax error: unexpected end of input). I've been looking over the previous script, but can't seem to identify the problem.
I'll paste the rest of the script below.
Cheers, Manda

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; } if ( correctGuess ) { document.write('<p>You guessed the number!</p>'); } else if ( parseInt(guess) > randomNumber ){ var lesserGuess = prompt('The number is less than this, guess again!') if (parseInt(lesserGuess) === randomNumber){ correctGuess = true; } } else if ( parseInt(guess) < randomNumber ){ var greaterGuess = prompt('The number is greater than this, guess again!') if (parseInt(greaterGuess) === randomNumber) { correctGuess = true; } if ( correctGuess ) { document.write('<p>You guessed the number!</p>');
} else { document.write('<p>Sorry. The number was ' + randomNumber + '. </p>'); }