๐ฏ Free game 19 of 20 ยท copy & paste
Number Guessing Game in HTML & JavaScript: Free Code + Demo
The computer picks a number from 1 to 100 and you have 7 tries to find it, with higher/lower hints and a bar that shows where the number can still be. Copy the code below, save it as number-guessing.html and open it in any browser.
- Type
- Word & quick
- Level
- Beginner
- Code
- 98 lines, 1 file
- Libraries
- None
- Works on
- Phones & computers
- You learn
- Forms, input validation and binary search
โฒ Play it right here. Type a number and press Enter or Guess.
What's in this free Number Guessing game
- Higher / lower and close hints
- A bar that narrows the range
- History of your guesses
- Input checks for bad numbers
- Change the range and tries
The code (98 lines, one file)
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Number Guessing Game</title>
<!--
Free HTML Number Guessing game โ one file, no libraries. Save it as number-guessing.html and open it in a browser.
From htmlgamescode.com (free to use in your own projects).
The computer picks a secret number. Guess it in as few tries as you can: it tells you "higher" or "lower".
-->
<style>
* { box-sizing: border-box; margin: 0; padding: 0; }
body {
min-height: 100vh; display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 16px;
background: #1b1446; color: #fff; font-family: system-ui, -apple-system, "Segoe UI", sans-serif; padding: 16px; text-align: center;
}
h1 { font-size: 26px; }
.sub { opacity: .8; }
.range { width: min(90vw, 380px); height: 26px; border-radius: 13px; background: #2a2170; position: relative; overflow: hidden; }
.range i { position: absolute; top: 0; bottom: 0; background: linear-gradient(90deg, #ff5fb1, #ffd23f); border-radius: 13px; transition: left .3s, width .3s; }
.range-labels { width: min(90vw, 380px); display: flex; justify-content: space-between; font-weight: 700; font-size: 14px; margin-top: -10px; }
form { display: flex; gap: 8px; }
input { width: 130px; padding: 12px; font: 800 24px system-ui, sans-serif; text-align: center; border: 0; border-radius: 14px; background: #fff; color: #1b1446; }
button { border: 0; border-radius: 14px; padding: 12px 22px; font: 700 18px system-ui, sans-serif; color: #fff; background: #36d35a; box-shadow: 0 4px 0 #1d9a3a; cursor: pointer; }
button:disabled { opacity: .5; }
.hint { font-size: 22px; font-weight: 900; min-height: 30px; }
.tries { opacity: .85; font-weight: 700; }
.history { display: flex; flex-wrap: wrap; justify-content: center; gap: 6px; max-width: 380px; }
.history span { padding: 4px 10px; border-radius: 10px; font-weight: 700; background: #2a2170; }
.history .up { background: #3f7bff; } .history .down { background: #ff8a3d; } .history .ok { background: #36d35a; }
#again[hidden] { display: none; }
</style>
</head>
<body>
<h1>๐ข Guess the number</h1>
<p class="sub" id="sub"></p>
<div class="range"><i id="bar"></i></div>
<div class="range-labels"><span id="lo"></span><span id="hi"></span></div>
<form id="form"><input id="guess" type="number" inputmode="numeric" autocomplete="off" aria-label="Your guess"><button id="go">Guess</button></form>
<div class="hint" id="hint">Type a number and press Guess</div>
<div class="tries" id="tries"></div>
<div class="history" id="history"></div>
<button id="again" hidden>Play again</button>
<script>
// ---- settings: change these to make it your own ----
const MIN = 1; // smallest possible number
const MAX = 100; // biggest possible number
const MAX_TRIES = 7; // guesses you get (7 is always enough for 1-100 if you halve the range)
const $ = id => document.getElementById(id);
let secret, tries, low, high, done;
function newGame() {
secret = MIN + Math.floor(Math.random() * (MAX - MIN + 1));
tries = 0; low = MIN; high = MAX; done = false;
$("sub").textContent = `I'm thinking of a number from ${MIN} to ${MAX}. You have ${MAX_TRIES} tries.`;
$("hint").textContent = "Type a number and press Guess";
$("history").innerHTML = ""; $("again").hidden = true; $("go").disabled = false; $("guess").value = "";
update(); $("guess").focus();
}
function guess(n) {
if (done) return;
if (!Number.isInteger(n) || n < MIN || n > MAX) { $("hint").textContent = `Enter a whole number from ${MIN} to ${MAX}`; return; }
tries++;
const chip = document.createElement("span");
chip.textContent = n;
if (n === secret) {
chip.className = "ok"; done = true; low = high = n;
$("hint").textContent = `๐ Yes! It was ${secret}. You got it in ${tries} ${tries === 1 ? "try" : "tries"}.`;
} else {
const far = Math.abs(n - secret) > (MAX - MIN) / 4 ? "" : Math.abs(n - secret) <= 3 ? " ๐ฅ Very close!" : " Getting warm.";
if (n < secret) { chip.className = "up"; low = Math.max(low, n + 1); $("hint").textContent = `โฌ Higher than ${n}.${far}`; }
else { chip.className = "down"; high = Math.min(high, n - 1); $("hint").textContent = `โฌ Lower than ${n}.${far}`; }
if (tries >= MAX_TRIES) { done = true; $("hint").textContent = `๐ต Out of tries. The number was ${secret}.`; }
}
$("history").appendChild(chip);
if (done) { $("again").hidden = false; $("go").disabled = true; }
$("guess").value = ""; update();
}
// the bar shows where the secret number can still be
function update() {
const span = MAX - MIN || 1;
$("bar").style.left = `${(low - MIN) / span * 100}%`;
$("bar").style.width = `${Math.max(2, (high - low) / span * 100)}%`;
$("lo").textContent = low; $("hi").textContent = high;
$("tries").textContent = done ? "" : `Tries left: ${MAX_TRIES - tries}`;
}
$("form").addEventListener("submit", e => { e.preventDefault(); guess(Number($("guess").value)); $("guess").focus(); });
$("again").addEventListener("click", newGame);
newGame();
</script>
</body>
</html>
How to use it
- Click Copy code (or Download).
- Paste it into any text editor and save it as
number-guessing.html. - Open the file in Chrome, Safari, Firefox or Edge. That's it, it runs offline.
How the code works
The secret
Math.random() picks a whole number between MIN and MAX.
Hints
Each guess moves the low or high end of the possible range, which the bar draws as a percentage.
Why 7 tries?
If you always guess the middle of the range (binary search), 100 numbers take at most 7 guesses.
Make it your own
All the settings are at the top of the script. Change a value, save and reload:
| Setting | Default | What it does |
|---|---|---|
MIN | 1 | smallest possible number |
MAX | 100 | biggest possible number |
MAX_TRIES | 7 | guesses you get (7 is always enough for 1-100 if you halve the range) |
Put it on your website
Upload number-guessing.html next to your page and add this where the game should appear:
<iframe src="number-guessing.html" width="460" height="620" style="border:0;max-width:100%" title="Number Guessing game"></iframe>
FAQ
Is this Number Guessing code really free?
Yes. Copy it and use it in your own website, school project or app, change it as much as you like and publish it, even commercially. A link back to HTML Games Code is welcome but not required.
How do I run the Number Guessing game?
Copy the code, paste it into a text editor, save the file as number-guessing.html and double-click it. It opens in your browser and works offline; there is nothing to install.
What are the controls?
Type a number and press Enter or Guess.
Does it work on phones?
Yes. It has touch controls and fits a phone screen, and it works with a keyboard and mouse on a computer.
Can I put it on my website?
Yes. Upload number-guessing.html and link to it, or show it inside an iframe on any page (see "Put it on your website" above).
Can I turn it into an Android app?
Yes. Any HTML game can be packaged as an Android APK. Our HTML Games APK Builder does it for our premium games, or you can use a tool like htmltoapk.com for this file.