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

Manda Schalin
Manda Schalin
1,346 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.

3 Answers

I Dilate
I Dilate
3,972 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,346 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>'); }

Nicholas Zavras
Nicholas Zavras
4,991 Points

So I've just got mine to work and it turned out to be a curly braces thing, I've spaced out the code you pasted here and I noticed you have one too many closing curly braces on one part, and you're missing a closing curly brace on another. I've typed in ERROR where I think the problem is

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!') ERROR NO CLOSING BRACE

if (parseInt(lesserGuess) === randomNumber){ correctGuess = true; }

}<--ERROR CLOSING BRACE else if ( parseInt(guess) < randomNumber ){ var greaterGuess = prompt('The number is greater than this, guess again!') ERROR NO CLOSING BRACE

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>'); }

Manda Schalin
Manda Schalin
1,346 Points

Thanks very much Nicholas, I'll have to check more carefully from now on. Cheers!