Number Guessing Game
Guess the secret number between 1 and 100. Get higher/lower hints. Fewest guesses wins.
How to play
- The computer picks a secret number from 1 to 100.
- Type your guess and click Guess (or press Enter).
- You'll get a "Too high" or "Too low" hint after each guess.
- Narrow the range with each guess — binary search (start at 50) finds it in ≤7 tries.
- Correct! shows your guess count; try to beat your best.
The computer picks a secret number between 1 and 100. Type your guess and receive higher/lower hints. Try to find it in as few guesses as possible — the mathematical minimum is 7 with a perfect binary search strategy.
How it works
The Number Guessing Game challenges you to identify a secret integer between 1 and 100. After each guess, the game tells you whether the secret number is higher or lower than your guess. The goal is to find the number in as few guesses as possible.
The mathematically optimal strategy is binary search: always guess the midpoint of the remaining possible range. Start with 50. If the answer is higher, guess 75 (midpoint of 51-100). If lower, guess 25 (midpoint of 1-49). Continue halving the range after each hint. This guarantees finding any number in at most 7 guesses out of a possible 100 values.
Binary search works because each guess eliminates exactly half of the remaining candidates regardless of the outcome. No other strategy can consistently do better against a fixed secret number with no additional information.
This game is an intuitive introduction to binary search, a fundamental algorithm used in software engineering for searching sorted data efficiently. It also demonstrates logarithmic complexity in an accessible way.
Worked example
Finding the number using binary search
- Guess 50. The game says Higher.
- Remaining range is 51 to 100. Guess the midpoint: 75. The game says Lower.
- Remaining range is 51 to 74. Guess 62. The game says Higher.
- Remaining range is 63 to 74. Guess 68. The game says Lower.
- Continue halving until a single number remains and guess it.
Secret number found within 7 guesses using binary search.
Common mistakes to avoid
- Guessing numbers close to previous guesses instead of jumping to the midpoint, which leaves large portions of the range unchecked.
- Ignoring the higher/lower hint and guessing in ascending or descending order, taking up to 100 guesses in the worst case.
- Guessing 1 or 100 as a first guess, which eliminates only one value if wrong instead of eliminating half the range.
Key terms
- Binary search
- An algorithm that finds a target value by repeatedly halving the search range based on whether the target is above or below the midpoint.
- Search range
- The current interval of numbers that could still be the secret, narrowed after each guess based on the higher/lower hint.
- Worst case
- The maximum number of guesses needed in the least favorable scenario; binary search guarantees at most 7 guesses for a 1-100 range.
Frequently asked questions
- What is the best strategy?
- Binary search: always guess the midpoint of the remaining range. Starting at 50, then adjusting by half each time, you can always find the number in at most 7 guesses.
- Is the number truly random?
- Yes — Math.random() generates a new number each game with equal probability for 1–100.