<html>
<head>
    <title>Guess the Number Game</title>
    <style>
        body {
            text-align: center;
        }
    </style>
</head>
<body>
    <h1>Guess the Number Game</h1>
<pre><code>&lt;h2&gt;I'm thinking of a number between 1 and 10&lt;/h2&gt;

&lt;input id='guess' type='text' placeholder='What's your guess?' /&gt;
&lt;button id='submit'&gt;Submit&lt;/button&gt;

&lt;div id='result'&gt;&lt;/div&gt;

&lt;script&gt;
    //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);
&lt;/script&gt;
</code></pre>
</body>
</html>
Simple HTML Number Guessing Game - Play Now!

原文地址: https://www.cveoy.top/t/topic/lml9 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录