I'm new to JavaScript and I'm doing a project for class. Haven't had any issues so far until this problem, where nothing prints when I enter the inputs. There are no changes at all when I enter the problems. This problem is just supposed to create a random number and see if the user guesses it correctly.
<script>
function playGame() {
var answer;
var x = Math.floor((Math.random() * 100) + 1);
var name = document.getElementById('name').value;
var guess = document.getElementById('guess').value;
if (x > guess) {
var answer = "Guess is too low!");
}
else if (x < guess) {
var answer = "Guess is too high!");
}
else if (x == guess) {
var answer = "You guessed correctly!");
}
document.getElementById('final').innerHTML = "Answer: " + answer;
</script>
<h2>Random Number Game by </h2>
<p>Enter Name:
<input id="name" type="text">
</p>
<p>Enter Guess: <input id="guess" type="number" min="1" max="100" onchange="playGame()"></p>
<h2 id="final"></h2>
Aucun commentaire:
Enregistrer un commentaire