Gioco dei Numeri: Indovinare i Numeri Casuali
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Gioco dei Numeri: Indovinare i Numeri Casuali</title>
<style>
<pre><code></style>
</code></pre>
</head>
<body>
<table>
<tr>
<td><input type="text" name="" id="InputA" placeholder="Indovina"></td>
<td><button></button></td>
</tr>
<tr>
<td><input type="text" name="" id="InputB" placeholder="Indovina"></td>
<td><button></button></td>
</tr> <tr>
<td><input type="text" name="" id="InputC" placeholder="Indovina"></td>
<td><button></button></td>
</tr>
<tr>
<td>Tentativi primo:</td>
<td><div class="risultato" id="Risultato A">0</div></td>
</tr>
<tr>
<td>Tentativi secondo:</td>
<td><div class="risultato" id="Risultato B">0</div></td>
</tr>
<tr>
<td>Tentativi terzo:</td>
<td><div class="risultato" id="Risultato C">0</div></td>
</tr>
<tr>
<td colspan="2"><button>Gioca ancora</button></td>
</tr>
</table>
<script>
// Function to generate a random number between min and max (inclusive)
function getRandomNumber(min, max) {
return Math.floor(Math.random() * (max - min + 1) + min);
}
<p>// Generate 3 random numbers
var numberA = getRandomNumber(1, 10);
var numberB = getRandomNumber(1, 10);
var numberC = getRandomNumber(1, 10);</p>
<p>// Get user inputs
var inputA = document.getElementById("InputA");
var inputB = document.getElementById("InputB");
var inputC = document.getElementById("InputC");</p>
<p>// Get result elements
var resultA = document.getElementById("Risultato A");
var resultB = document.getElementById("Risultato B");
var resultC = document.getElementById("Risultato C");</p>
<p>// Set initial result values to 0
resultA.innerText = 0;
resultB.innerText = 0;
resultC.innerText = 0;</p>
<p>// Function to check user's input against the random number and update the result
function checkNumber(input, randomNumber, result) {
if (input.value == randomNumber) {
result.innerText = "Vincita!";
} else {
result.innerText = "Tentativo non riuscito";
}
}</p>
<p>// Event listeners for the buttons
document.querySelector("button:nth-of-type(1)").addEventListener("click", function() {
checkNumber(inputA, numberA, resultA);
});</p>
<p>document.querySelector("button:nth-of-type(2)").addEventListener("click", function() {
checkNumber(inputB, numberB, resultB);
});</p>
<p>document.querySelector("button:nth-of-type(3)").addEventListener("click", function() {
checkNumber(inputC, numberC, resultC);
});</p>
<p>// Reset the game
document.querySelector("button:nth-of-type(4)").addEventListener("click", function() {
numberA = getRandomNumber(1, 10);
numberB = getRandomNumber(1, 10);
numberC = getRandomNumber(1, 10);</p>
<pre><code>inputA.value = "";
inputB.value = "";
inputC.value = "";
resultA.innerText = 0;
resultB.innerText = 0;
resultC.innerText = 0;
</code></pre>
<p>});
</script></p>
</body>
</html>
原文地址: https://www.cveoy.top/t/topic/pcYL 著作权归作者所有。请勿转载和采集!