Guess the Number: Your First Program
Build a small number-guessing game in your web browser, one feature at a time, and learn the ideas every program is made of, with nothing to install and no programming experience needed.
Every app on your phone, every website and every game started as a plain file of instructions that somebody typed. This series shows you how that works by having you write one: a small game called Guess the Number.
The computer picks a secret number from 1 to 100. The player types a guess, and the game answers “Too high!” or “Too low!” until they find it, or run out of lives. It is a game a child can play, and it holds almost every idea programmers use every day: showing things on a screen, remembering values, reacting to a click, making decisions, picking random numbers, repeating work and organising code into named pieces.
You build it in nine parts. Each part adds one feature you can see and introduces one or two new ideas, explained in plain words and pictures before you type anything. Every part has a working preview of the game as it should be at that point, so you can check yours against it, and a mistake to make on purpose, so error messages become familiar instead of frightening. Each part ends with a small challenge of your own, with a solution you can open if you get stuck.
You need no experience and nothing to install. The whole game lives in one file called game.html, written in any text editor, even the one your computer already has, and run in the browser you are using now. The language is JavaScript, the one every web browser understands. By the end of part 9 the game is finished, it is yours, and you can send the file to a friend to play.
The route
Read in order. Each part stands on its own.- 01 Part 01
Before We Start: Your Tools
A project folder, a plain-text file called game.html, the edit, save, refresh rhythm you will use in every part, and where the browser shows its error messages.
7 sections · 1 interactive - 02 Part 02
Your First Line of Code
What a program is, the script element, and your first event, a Guess button that changes a line of text when you click it.
6 sections · 1 interactive - 03 Part 03
Output and Variables
Variables as labelled boxes, the difference between text and numbers, joining text with +, and why the game always shows text with textContent.
6 sections · 1 interactive - 04 Part 04
Reading the Player's Guess
A number box for the guess, reading what is in it, the surprise that "5" + 5 makes "55", and turning text into a number with Number().
6 sections · 1 interactive - 05 Part 05
Making Decisions
Comparisons and their true or false answers, if, else if and else as a fork in the road, a guard against empty boxes, and the difference between = and ===.
7 sections · 2 interactives - 06 Part 06
Randomness and Counting
Math.random() and the four steps that turn it into a number from 1 to 100, a counter that goes up by one, and why some boxes should never change.
7 sections · 2 interactives - 07 Part 07
Loops: Lives and History
The for loop as laps of a track, a first look at arrays with push and for…of, a game that finally ends, and the one loop you should never run.
6 sections · 2 interactives - 08 Part 08
Functions: Organising the Game
Defining and calling functions, parameters as boxes that fill on each call, reorganising the game into six functions, and the Play again button that comes almost for free.
6 sections · 2 interactives - 09 Part 09
Polish and Share
A little CSS for colour and centring, the Enter key, a box that is ready for the next guess, the strategy behind seven lives, and the finished game in one file you can share.
7 sections · 2 interactives