Simple HTML Number Guessing Game - Play Now!
<html>
<head>
<title>Guess the Number Game</title>
<style>
body {
text-align: center;
}
</style>
</head>
<body>
<h1>Guess the Number Game</h1>
<pre><code><h2>I'm thinking of a number between 1 and 10</h2>
<input id='guess' type='text' placeholder='What's your guess?' />
<button id='submit'>Submit</button>
<div id='result'></div>
<script>
//Get the elements
const guessInput = document.getElementById('guess');
const submitBtn = document.getElementById('submit');
const resultDiv = document.getElementById('result');
// Generate a random number
let randomNumber = Math.floor(Math.random() * 10) + 1;
//Function to check the number
function checkNumber() {
// Get the value of the guess input
let guess = parseInt(guessInput.value);
// Check if the number is equal to the random number
if (guess === randomNumber) {
resultDiv.innerHTML = 'You guessed it! The number was ' + randomNumber;
} else {
resultDiv.innerHTML = 'Wrong! Try again!';
}
}
// Listen for a click on the submit button
submitBtn.addEventListener('click', checkNumber);
</script>
</code></pre>
</body>
</html>
原文地址: https://www.cveoy.top/t/topic/lml9 著作权归作者所有。请勿转载和采集!