Play the Guessing Game: A Simple HTML Number Game
<!DOCTYPE html>
<html>
<head>
<title>Guess the Number Game</title>
<script type="text/javascript">
//Variable to store the secret number
var secretNumber;
//Variable to store the number of attempts
var numAttempts = 0;
//Generate a random number between 1 and 10
secretNumber = Math.floor(Math.random() * 10) + 1;
//Get a number from the user
function guessNumber(){
//Increment the number of attempts
numAttempts++;
//Get the value from the text box
var userGuess = document.getElementById('guess').value;
//Check if the number is correct
if(userGuess == secretNumber){
alert('Congratulations - You guessed the number in ' + numAttempts + ' attempts!');
} else if(userGuess < secretNumber){
alert('Your guess is too low. Try again!');
} else {
alert('Your guess is too high. Try again!');
}
}
</script>
</head>
<body>
<h1>Guess the Number Game</h1>
<p>I'm thinking of a number between 1 and 10. Can you guess it?</p>
<input type="text" id="guess" />
<button onclick="guessNumber()">Guess</button>
</body>
</html>
原文地址: https://www.cveoy.top/t/topic/lmce 著作权归作者所有。请勿转载和采集!