Simple HTML Guessing Game - Code Example
<html>
<head>
<title>My HTML Game</title>
</head>
<body>
<h1>My HTML Game</h1>
<p>Guess the secret number!</p>
<script>
// Get a random number between 1 and 10
var secretNumber = Math.floor(Math.random() * 10) + 1;
// Ask the user to guess the number
var guessString = prompt('Guess the number between 1 and 10');
// Convert the string to an int
var guess = parseInt(guessString);
// Check if the guess is correct
if (guess === secretNumber) {
alert('You guessed it!');
}
else {
alert('Sorry, that wasn't the right answer. The secret number was ' + secretNumber);
}
</script>
</body>
</html>
原文地址: https://www.cveoy.top/t/topic/lmlE 著作权归作者所有。请勿转载和采集!