How do i keep picking random numbers until the timer times out?

1 day ago 2
ARTICLE AD BOX

I have a script that allow the user to match the targetNumbers in any order in each input fields however when all the inputs field is locked (incorrect) the game reset and try again. I would like to keep continuing the picking numbers until the timer times out or all winning numbers correct (locked). I think its function checkWinCondition that probably needs to be fix. Is there away to fix this?

Here is my code below.

let timeLeft = 5; let timerInterval; let shuffleInterval; let currentInputIndex = 0; let targetNumbers = []; const timerDisplay = document.getElementById('timer'); const inputs = document.querySelectorAll('.rand-input'); const resultDisplay = document.getElementById('result'); const getRandom = () => Math.floor(Math.random() * 2); const mainBtn = document.getElementById('mainBtn'); const message = document.getElementById('message'); function handleButtonClick() { if (mainBtn.innerText === "Start") { startApp(); } else if (mainBtn.innerText === "Try-Again") { resetGame(); } else if (mainBtn.innerText === "Pick") { lockInput(); } } message.innerText = "Press start to play!"; mainBtn.innerText = "Start"; mainBtn.className = "start"; function startApp() { clearInterval(timerInterval); clearInterval(shuffleInterval); timeLeft = 5; currentInputIndex = 0; timerDisplay.innerText = timeLeft; message.innerText = "Match the numbers!"; mainBtn.innerText = "Pick"; mainBtn.className = "pick"; // 1. Generate new target numbers and display them targetNumbers = [getRandom(), getRandom(), getRandom()]; resultDisplay.innerText = targetNumbers.join(' '); inputs.forEach(input => { input.value = "-"; input.classList.remove('locked', 'correct', 'incorrect'); }); // 2. Timer timerInterval = setInterval(() => { timeLeft--; timerDisplay.innerText = timeLeft; if (timeLeft <= 0) { stopApp("Time's up!"); } }, 1000); // 3. Shuffler shuffleInterval = setInterval(() => { if (currentInputIndex < inputs.length) { inputs[currentInputIndex].value = getRandom(); } }, 100); } function lockInput() { if (currentInputIndex < inputs.length) { const currentInput = inputs[currentInputIndex]; const currentVal = parseInt(currentInput.value); // 1. Check if the value exists in targetNumbers (in any order) const matchIndex = targetNumbers.indexOf(currentVal); if (matchIndex !== -1) { // Correct match: Highlight green currentInput.classList.add('correct'); // Remove the matched number so it cannot be used again targetNumbers.splice(matchIndex, 1); } else { // Incorrect: Highlight red currentInput.classList.add('incorrect'); } // Lock current currentInput.classList.add('locked'); currentInputIndex++; // 2. Check if all inputs are locked if (currentInputIndex >= inputs.length) { checkWinCondition(); } } } function checkWinCondition() { clearInterval(timerInterval); clearInterval(shuffleInterval); mainBtn.innerText = "Try-Again"; mainBtn.className = "try-again"; // Check if all target numbers were matched (i.e., array is empty) if (targetNumbers.length === 0) { message.innerText = "💰💰💰 You Win! "; } else { message.innerText = "No-match!"; // Show missed numbers message.innerText += ` Missed: ${targetNumbers.join(' ')}`; setTimeout(() => { currentInputIndex = 0; inputs.forEach(input => { input.classList.remove('locked', 'correct', 'incorrect'); input.value = ''; }); }, 5000); } } function stopApp(msg) { clearInterval(timerInterval); clearInterval(shuffleInterval); message.innerText = msg; mainBtn.innerText = "Try-Again"; mainBtn.className = "try-again"; inputs.forEach(input => input.classList.add('locked', 'correct', 'incorrect')); } function resetGame() { clearInterval(timerInterval); clearInterval(shuffleInterval); timeLeft = 5; currentInputIndex = 0; timerDisplay.innerText = timeLeft; message.innerText = ""; resultDisplay.innerText = "- - -"; inputs.forEach(input => { input.value = ""; input.classList.remove('locked', 'correct', 'incorrect'); }); mainBtn.innerText = "Start"; mainBtn.className = "start"; } <div class="container"> <h2>Time remaining: <span id="timer">5</span>s</h2> </div> <div class="game-wrapper"> <h3 id="result" class="result1">0 0 0</h3> </div> <div class="inputs-grid"> <input type="text" class="rand-input" readonly value=""> <input type="text" class="rand-input" readonly value=""> <input type="text" class="rand-input" readonly value=""> </div> <div class="controls"> <button id="mainBtn" onclick="handleButtonClick()">Start</button> </div> <p id="message"></p>
Read Entire Article