Gioco dei numeri: indovina i 3 numeri casuali
// Get the input elements and the result elements
const inputA = document.getElementById('InputA');
const inputB = document.getElementById('InputB');
const inputC = document.getElementById('InputC');
const resultA = document.getElementById('Risultato A');
const resultB = document.getElementById('Risultato B');
const resultC = document.getElementById('Risultato C');
// Generate 3 random numbers between 1 and 10
const randomNumberA = Math.floor(Math.random() * 10) + 1;
const randomNumberB = Math.floor(Math.random() * 10) + 1;
const randomNumberC = Math.floor(Math.random() * 10) + 1;
// Function to check the user's input and display the result
function checkInput(input, randomNumber, result) {
const userGuess = parseInt(input.value);
if (userGuess === randomNumber) {
result.textContent = 'Vincita!';
} else {
result.textContent = 'Tentativo sbagliato!';
}
}
// Add event listeners to the buttons
document.querySelector('button:nth-of-type(1)').addEventListener('click', function() {
checkInput(inputA, randomNumberA, resultA);
});
document.querySelector('button:nth-of-type(2)').addEventListener('click', function() {
checkInput(inputB, randomNumberB, resultB);
});
document.querySelector('button:nth-of-type(3)').addEventListener('click', function() {
checkInput(inputC, randomNumberC, resultC);
});
// Function to reset the game
function resetGame() {
inputA.value = '';
inputB.value = '';
inputC.value = '';
resultA.textContent = '0';
resultB.textContent = '0';
resultC.textContent = '0';
randomNumberA = Math.floor(Math.random() * 10) + 1;
randomNumberB = Math.floor(Math.random() * 10) + 1;
randomNumberC = Math.floor(Math.random() * 10) + 1;
}
// Add event listener to the 'Gioca ancora' button
document.querySelector('button:nth-of-type(4)').addEventListener('click', resetGame);
Note: Make sure to add this JavaScript code within the <script></script> tags in your HTML file.
原文地址: https://www.cveoy.top/t/topic/pcZi 著作权归作者所有。请勿转载和采集!