From 269d48a359021dce4f89454ea7cf980eecef37ff Mon Sep 17 00:00:00 2001 From: Olena Salnikova Date: Tue, 3 Mar 2026 20:43:12 -0500 Subject: [PATCH] 10 bugs found and fixed --- index.js | 28 +++++++++++++++++----------- 1 file changed, 17 insertions(+), 11 deletions(-) diff --git a/index.js b/index.js index 4c2a94e..f951588 100644 --- a/index.js +++ b/index.js @@ -18,6 +18,7 @@ const maxNumberOfAttempts = 5; // <- 32 // > getRandomNumber(1, 50) // <- 11 + function getRandomNumber(min, max) { return Math.floor(Math.random() * (max - min)) + min; } @@ -25,13 +26,18 @@ function getRandomNumber(min, max) { function checkGuess() { // Get value from guess input element const guess = parseInt(guessInput.value, 10); - attempts = attempts + 1; - + hideAllMessages(); + if ( guess < 1 || guess > 99 || isNaN(guess) ) { // Stretch Goals: Input validation + console.log('Invalid guess'); + return; + } + attempts = attempts + 1; + if (guess === targetNumber) { numberOfGuessesMessage.style.display = ''; - numberOfGuessesMessage.innerHTML = `You made ${attempts} guesses`; + numberOfGuessesMessage.innerHTML = `You made ${attempts} ${attempts === 1 ? 'guess' : 'guesses'}`; // Stretch Goals: If there is only one guess left, it should say "guess" (singular) instead of "guesses" (plural) correctMessage.style.display = ''; @@ -43,16 +49,16 @@ function checkGuess() { if (guess < targetNumber) { tooLowMessage.style.display = ''; } else { - tooLowMessage.style.display = ''; + tooHighMessage.style.display = ''; // Was an error here, should be tooHighMessage not tooLowMessage } const remainingAttempts = maxNumberOfAttempts - attempts; numberOfGuessesMessage.style.display = ''; - numberOfGuessesMessage.innerHTML = `You guessed ${guess}.
${remainingAttempts} guesses remaining`; + numberOfGuessesMessage.innerHTML = `You guessed ${guess}.
${remainingAttempts} ${remainingAttempts === 1 ? 'guess' : 'guesses'} remaining`; // Stretch Goals: If there is only one guess left, it should say "guess" (singular) instead of "guesses" (plural) } - if (attempts ==== maxNumberOfAttempts) { + if (attempts === maxNumberOfAttempts) { // Was an error here, should be === not ==== submitButton.disabled = true; guessInput.disabled = true; } @@ -63,21 +69,21 @@ function checkGuess() { } function hideAllMessages() { - for (let elementIndex = 0; elementIndex <= messages.length; elementIndex++) { + for (let elementIndex = 0; elementIndex < messages.length; elementIndex++) { // Was an error here, should be elementIndex < messages.length not elementIndex <= messages.length messages[elementIndex].style.display = 'none'; } } -funtion setup() { +function setup() { // Was an error here, should be function not funtion // Get random number - targetNumber = getRandomNumber(1, 100); + targetNumber = getRandomNumber(1, 99); // Was an error here, should be 99 not 100 console.log(`target number: ${targetNumber}`); // Reset number of attempts - maxNumberOfAttempts = 0; + attempts = 0; // Was an error here, should be attempts not maxNumberOfAttempts // Enable the input and submit button - submitButton.disabeld = false; + submitButton.disabled = false; // Was an error here, should be disabled not disabeld guessInput.disabled = false; hideAllMessages();