Fix bugs in guessing game and maintain reset functionality#267
Fix bugs in guessing game and maintain reset functionality#267shanny2022 wants to merge 4 commits into
Conversation
There was a problem hiding this comment.
Pull request overview
This PR fixes the guessing game’s core gameplay flow by correcting message handling, input validation, and wiring guesses through a form submission while preserving reset-driven replay.
Changes:
- Added a
<form>wrapper so guesses can be submitted via Enter key and handled viasubmitevent. - Refactored game logic to reset state in
setup(), hide/show messages consistently, and fix end-of-game behavior. - Added input validation and improved guess feedback text (including pluralization).
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 3 comments.
| File | Description |
|---|---|
| index.js | Introduces form submit handling, input validation, improved message toggling, and a consolidated setup() reset flow. |
| index.html | Wraps guess input/button in a form and adds instructional comments to match the updated interaction model. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
You can also share your feedback on Copilot code review. Take the survey.
| 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'; |
There was a problem hiding this comment.
parseInt() will truncate decimals (e.g., 1.9 becomes 1) and can accept partially numeric strings, so some invalid inputs may be treated as valid guesses. Consider using Number(guessInput.value) and validating with Number.isInteger() (and range) so non-integers are rejected rather than coerced.
| guessInput.value = ''; | ||
|
|
||
| resetButton.style.display = ''; | ||
| } |
There was a problem hiding this comment.
resetButton.style.display = '' is set after every valid guess, but setup() hides the reset button and the UI copy suggests reset is only intended after win/max guesses. Either only show reset when the game is over (win/lose), or update the behavior/copy to match the intended UX.
| Would you like to play again? | ||
| </p> | ||
| </div> | ||
| <!-- Reset starts a brand-new game after a win or after using all guesses. --> |
There was a problem hiding this comment.
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.
| <!-- Reset starts a brand-new game after a win or after using all guesses. --> | |
| <!-- Reset lets the player start a brand-new game at any time. --> |
No description provided.