I Spy Game - HTML & JavaScript Code
<html>
<head>
<title>I Spy Game</title>
<script type="text/javascript">
// Start the game by picking a random item
const items = ['Apple', 'Car', 'Tree'];
let randomIndex = Math.floor(Math.random() * items.length);
let currentItem = items[randomIndex];
<pre><code> // Get the user's guess
function getGuess() {
let guess = document.getElementById('guess').value;
if (guess === currentItem) {
alert('You guessed correctly!');
resetGame();
} else {
alert('Sorry, that's not quite right. Try again!');
}
}
// Reset the game
function resetGame() {
randomIndex = Math.floor(Math.random() * items.length);
currentItem = items[randomIndex];
document.getElementById('guess').value = '';
}
</script>
</code></pre>
</head>
<body>
<h1>I Spy Game</h1>
<p>Can you guess what I'm thinking of?</p>
<input type="text" id="guess" />
<button onclick="getGuess()">Submit Guess</button>
</body>
</html>
原文地址: https://www.cveoy.top/t/topic/lnPS 著作权归作者所有。请勿转载和采集!