Simple HTML Game: Guess the Number - Play Now!
<!DOCTYPE html>
<html>
<head>
<title>Simple HTML Game: Guess the Number</title>
<script>
//This function will generate a random number between 1 and 10
function getRandomNumber(){
return Math.floor(Math.random() * 10 + 1);
}
</script>
</head>
<body>
<h1>Simple HTML Game: Guess the Number</h1>
<p>Guess the number between 1 and 10 to win!</p>
<form action="">
<input type="text" name="guess" id="guessInput" placeholder="Your Guess">
<input type="button" value="Submit" onclick="checkGuess()">
</form>
<div>
<p id="message"></p>
</div>
<script>
let randomNumber = getRandomNumber();
//This function will check if the user's guess is correct
function checkGuess(){
let message = document.getElementById('message');
let guess = document.getElementById('guessInput').value;
if(guess == randomNumber){
message.innerHTML = 'Congratulations! You guessed correctly!';
} else {
message.innerHTML = 'Sorry, try again.';
}
}
</script>
</body>
</html>
原文地址: https://www.cveoy.top/t/topic/lml0 著作权归作者所有。请勿转载和采集!