Homegamescode Guess the Number Game HTML CSS & Js Source Code By Mr Secret February 04, 2024 Guess the Number Game HTML CSS & Js Source Code Copy Download <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <style> .container { max-width: 600px; margin: 50px auto; background-color: #fff; padding: 20px; border-radius: 10px; box-shadow: 0 0 10px rgba(0, 0, 0, 0.1); } h1 { color: #333; } #result { margin-top: 20px; font-size: 18px; } input { padding: 10px; font-size: 16px; margin-top: 10px; } button { background-color: #3498db; color: #fff; padding: 10px 20px; font-size: 16px; border: none; border-radius: 5px; cursor: pointer; margin-top: 10px; } button:hover { background-color: #2980b9; } </style> <title>Guess the Number Game</title> </head> <body> <div class="container"> <h1>Guess the Number</h1> <p>Guess a number between 1 and 100:</p> <input type="number" id="guess" min="1" max="100"> <button onclick="checkGuess()">Submit Guess</button> <div id="result"></div> </div> <script> var secretNumber = Math.floor(Math.random() * 100) + 1; var attempts = 0; function checkGuess() { var userGuess = parseInt(document.getElementById("guess").value); if (isNaN(userGuess) || userGuess < 1 || userGuess > 100) { document.getElementById("result").innerText = "Please enter a valid number between 1 and 100."; } else { attempts++; if (userGuess === secretNumber) { document.getElementById("result").innerText = `Congratulations! You guessed the correct number in ${attempts} attempts.`; resetGame(); } else if (userGuess < secretNumber) { document.getElementById("result").innerText = "Too low! Try again."; } else { document.getElementById("result").innerText = "Too high! Try again."; } } } function resetGame() { secretNumber = Math.floor(Math.random() * 100) + 1; attempts = 0; } </script> </body> </html> Tags: gamescode Facebook Twitter