-
Notifications
You must be signed in to change notification settings - Fork 336
Fix bugs in guessing game and maintain reset functionality #267
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,3 +1,5 @@ | ||
| // Cache the key elements the game updates after each guess. | ||
| const guessForm = document.getElementById('guess-form'); | ||
| const guessInput = document.getElementById('guess'); | ||
| const submitButton = document.getElementById('submit'); | ||
| const resetButton = document.getElementById('reset'); | ||
|
|
@@ -22,69 +24,74 @@ function getRandomNumber(min, max) { | |
| return Math.floor(Math.random() * (max - min)) + min; | ||
| } | ||
|
|
||
| function checkGuess() { | ||
| // Get value from guess input element | ||
| const guess = parseInt(guessInput.value, 10); | ||
| attempts = attempts + 1; | ||
| // Hides every feedback message so only the current game state is visible. | ||
| function hideAllMessages() { | ||
| for (let elementIndex = 0; elementIndex < messages.length; elementIndex += 1) { | ||
| messages[elementIndex].style.display = 'none'; | ||
| } | ||
| } | ||
|
|
||
| hideAllMessages(); | ||
| function getGuessWord(remainingAttempts) { | ||
| return remainingAttempts === 1 ? 'guess' : 'guesses'; | ||
| } | ||
|
|
||
| if (guess === targetNumber) { | ||
| numberOfGuessesMessage.style.display = ''; | ||
| numberOfGuessesMessage.innerHTML = `You made ${attempts} guesses`; | ||
| // Resets the game to the initial state with a new random target number. | ||
| function setup() { | ||
| targetNumber = getRandomNumber(1, 100); | ||
| console.log(`target number: ${targetNumber}`); | ||
|
|
||
| correctMessage.style.display = ''; | ||
| attempts = 0; | ||
| guessInput.value = ''; | ||
| submitButton.disabled = false; | ||
| guessInput.disabled = false; | ||
|
|
||
| submitButton.disabled = true; | ||
| guessInput.disabled = true; | ||
| } | ||
| hideAllMessages(); | ||
| resetButton.style.display = 'none'; | ||
| } | ||
|
|
||
| if (guess !== targetNumber) { | ||
| if (guess < targetNumber) { | ||
| tooLowMessage.style.display = ''; | ||
| } else { | ||
| tooLowMessage.style.display = ''; | ||
| } | ||
| // Checks the player's guess and updates the visible game messages. | ||
| function checkGuess(event) { | ||
| event.preventDefault(); | ||
|
|
||
| const remainingAttempts = maxNumberOfAttempts - attempts; | ||
| const guess = parseInt(guessInput.value, 10); | ||
|
|
||
| if (!Number.isInteger(guess) || guess < 1 || guess > 99) { | ||
| hideAllMessages(); | ||
| numberOfGuessesMessage.style.display = ''; | ||
| numberOfGuessesMessage.innerHTML = `You guessed ${guess}. <br> ${remainingAttempts} guesses remaining`; | ||
| numberOfGuessesMessage.innerHTML = 'Enter a number from 1 to 99.'; | ||
| resetButton.style.display = attempts > 0 ? '' : 'none'; | ||
|
Comment on lines
+56
to
+62
|
||
| return; | ||
| } | ||
|
|
||
| if (attempts ==== maxNumberOfAttempts) { | ||
| attempts += 1; | ||
|
|
||
| hideAllMessages(); | ||
|
|
||
| const remainingAttempts = maxNumberOfAttempts - attempts; | ||
|
|
||
| numberOfGuessesMessage.style.display = ''; | ||
| numberOfGuessesMessage.innerHTML = `You guessed ${guess}. <br> ${remainingAttempts} ${getGuessWord(remainingAttempts)} remaining`; | ||
|
|
||
| if (guess === targetNumber) { | ||
| correctMessage.style.display = ''; | ||
| submitButton.disabled = true; | ||
| guessInput.disabled = true; | ||
| } else if (attempts === maxNumberOfAttempts) { | ||
| maxGuessesMessage.style.display = ''; | ||
| numberOfGuessesMessage.innerHTML = `You guessed ${guess}. <br> 0 guesses remaining`; | ||
| submitButton.disabled = true; | ||
| guessInput.disabled = true; | ||
| } else if (guess < targetNumber) { | ||
| tooLowMessage.style.display = ''; | ||
| } else { | ||
| tooHighMessage.style.display = ''; | ||
| } | ||
|
|
||
| guessInput.value = ''; | ||
|
|
||
| resetButton.style.display = ''; | ||
| } | ||
|
Comment on lines
90
to
92
|
||
|
|
||
| function hideAllMessages() { | ||
| for (let elementIndex = 0; elementIndex <= messages.length; elementIndex++) { | ||
| messages[elementIndex].style.display = 'none'; | ||
| } | ||
| } | ||
|
|
||
| funtion setup() { | ||
| // Get random number | ||
| targetNumber = getRandomNumber(1, 100); | ||
| console.log(`target number: ${targetNumber}`); | ||
|
|
||
| // Reset number of attempts | ||
| maxNumberOfAttempts = 0; | ||
|
|
||
| // Enable the input and submit button | ||
| submitButton.disabeld = false; | ||
| guessInput.disabled = false; | ||
|
|
||
| hideAllMessages(); | ||
| resetButton.style.display = 'none'; | ||
| } | ||
|
|
||
| submitButton.addEventListener('click', checkGuess); | ||
| guessForm.addEventListener('submit', checkGuess); | ||
| resetButton.addEventListener('click', setup); | ||
|
|
||
| setup(); | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The comment says reset starts a new game "after a win or after using all guesses", but the current JS shows the reset button after any valid guess. Update this comment or adjust the reset-button visibility logic so the code and instructions stay consistent.