Skip to main content

Number Guessing Game

Problem Statement

Create a program that allows the user to guess a number between 1 and 100. The program should have a simple user interface that includes an input field for the guess and a button to submit the guess. When the user clicks the button, the program should display a message indicating whether the guess is too low, low, almost, correct, high, or too high.

  • Correct: The guess is exactly the correct number.
  • Almost: The guess is within 5 of the correct number (but not correct).
  • Low: The guess is more than 5 but at most 10 below the correct number.
  • High: The guess is more than 5 but at most 10 above the correct number.
  • Too low: The guess is more than 10 below the correct number.
  • Too high: The guess is more than 10 above the correct number.

Acceptance Criteria

  • The program should have an HTML file with the required user interface elements (input field, button) styled with CSS.
  • The program should have a JavaScript function that compares the guess to the correct number.
  • When the user clicks the button, the program should compare the guess to the correct number and display the matching message from the rules above.
  • The program should handle any errors (e.g., submitting an empty or non-numeric guess) gracefully and display an error message to the user.

Hints

  • Use the Math.random() function to generate a random number between 0 and 1, then scale it to the 1-100 range. i.e. Math.floor(Math.random() * 100) + 1
  • Use Math.abs() to get the distance between the guess and the correct number. i.e. Math.abs(guess - number)