Interactive JavaScript game
Number Guessing Game
Choose a difficulty, then find the secret number before you run out of guesses.
Number range
Attempts per game10
Previous guesses
- Your guesses will appear here.
Learn by building
How to Build This JavaScript Number Guessing Game
The game chooses a secret number, reads each guess from the page, and updates its feedback and remaining attempts. These core JavaScript ideas make the interaction work:
- Math.random() creates a random decimal that can be used to choose the secret number.
- Math.floor() rounds the scaled random value down to an integer in the selected range.
- DOM manipulation updates the status, attempts counter, and guess history.
- addEventListener() responds to form submissions, difficulty changes, and reset clicks.
- State management keeps the secret number, attempts, and guesses in sync across turns.
1. Add the game controls
Use a form so both the Guess button and the Enter key submit the same guess.
<form id="guess-form">
<label for="guess-input">Your guess</label>
<input id="guess-input" type="number" min="1" max="100">
<button type="submit">Guess</button>
</form>
<p id="status-banner" role="status" aria-live="polite"></p>
<ol id="guess-history"></ol>2. Style the feedback
CSS classes let JavaScript communicate whether a guess is high, low, or close to the answer.
.status-banner {
padding: 1rem;
border-radius: 0.75rem;
background: var(--surface);
color: var(--text);
}
.guess-entry--high { color: #b42318; }
.guess-entry--low { color: #2563a6; }
.status-banner--success { color: #237a35; }3. Choose a number and check each guess
Keep the secret number and attempts in variables. Compare the submitted integer, update the DOM, and save a winning score for later games.
const form = document.querySelector("#guess-form");
const input = document.querySelector("#guess-input");
const status = document.querySelector("#status-banner");
const attempts = document.querySelector("#attempts-display");
const history = document.querySelector("#guess-history");
const max = 50;
const secretNumber = Math.floor(Math.random() * max) + 1;
let attemptsLeft = 10;
form.addEventListener("submit", (event) => {
event.preventDefault();
const rawGuess = input.value.trim();
const guess = Number(rawGuess);
if (!rawGuess || !Number.isInteger(guess) || guess < 1 || guess > max) {
status.textContent = `Enter a whole number from 1 to ${max}.`;
return;
}
attemptsLeft -= 1;
attempts.textContent = attemptsLeft;
if (guess === secretNumber) {
status.textContent = `Correct! You won in ${10 - attemptsLeft} attempts!`;
const best = Number(localStorage.getItem("number-game-best")) || Infinity;
if (10 - attemptsLeft < best) localStorage.setItem("number-game-best", 10 - attemptsLeft);
input.disabled = true;
return;
}
const direction = guess > secretNumber ? "high" : "low";
const entry = document.createElement("li");
entry.className = `guess-entry--${direction}`;
entry.textContent = `${guess} — Too ${direction}`;
history.prepend(entry);
status.textContent = Math.abs(guess - secretNumber) <= 5
? "Getting Warm!"
: direction === "high" ? "Too High!" : "Too Low!";
});