First, your JS
function isn’t closed properly with an ending bracket }
, causing syntax errors. Secondly, you have a character double quote that isn’t supported in the code “
instead of using "
, which is also causing runtime errors. Also, you aren’t closing your header tags (<h1>, <h2>
) etc.
function guessNumber() {
var playerGuess = document.getElementById("guessInput").value;
if (playerGuess == "" || playerGuess < 1 || playerGuess > 100) {
document.getElementById("output").innerHTML = "Please enter a number 1 - 100";
return;
}
var computerChoice = Math.floor(Math.random() * 10) + 1;
if (playerGuess == computerChoice) {
document.getElementById("output").innerHTML =
"You win: both you and the computer picked " + playerGuess;
} else {
document.getElementById("output").innerHTML =
"Too bad, you picked " + playerGuess + " and the computer picked " + computerChoice;
}
}
<h1>Guessing Game
</h1>
<br/>
<h2>The computer will choose a number between 1 and 100
</h2>
<br/>
<h2>You have to try and guess what it picked
</h2>
<br/>
<span>Input your guess</span>
<input type="number" min="1" max="100" id="guessInput" />
<br/>
<button onClick="guessNumber();">Guess</button>
<p id="output"></p>
solved Random number game isnt functioning