Part 01 / 09

Before We Start: Your Tools

By the end of this part, a file you made yourself will open in your browser and show the words Guess the Number.

7 sections 1 interactive
§ 1.1

Two tools you already have

Over the next nine parts you will build a small game that runs in a web browser. The computer picks a secret number from 1 to 100, and the player has seven tries to find it, with a hint after every guess: too high, or too low. By the end, the whole game is one file that you wrote yourself and can send to a friend.

You don’t need to install anything. You need two programs: a text editor for writing, and a web browser for running what you wrote. Use whichever text editor you like, as long as it can save plain text. Every computer already has one: Notepad on Windows, TextEdit on a Mac. Any up-to-date browser will do, such as Chrome, Edge, Firefox or Safari.

If you’d like an editor made for writing code, which makes the work easier but is entirely optional, read the section about it below before you save your first file.

What you write is : instructions in a language a computer can follow. This series uses two languages that every browser understands. describes what is on the page: a heading, a box to type in, a button. JavaScript makes the page do things, such as answer a click or pick a random number. You will write a few lines of HTML in this part and your first JavaScript in part 2. The ideas you meet along the way work in every programming language, so nothing you learn here is wasted.

One more thing before you start: nothing you do in this series can break your computer. Your game runs inside a browser tab, and browsers keep pages walled off from the rest of the machine. Chrome’s design documents describe this sandbox as letting code run “that cannot make persistent changes to the computer”. The worst a mistake can do is stop your own page from working until you fix it.

Phones and tablets are fine for reading these articles, but to build the game you will need a computer with a keyboard.

§ 1.2

The starter file

Every part of this series changes the same file, called game.html. Here is how it starts. You will save it in a moment; first, look at what it says.

game.html
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Guess the Number</title>
</head>
<body>
<h1>Guess the Number</h1>
</body>
</html>

The words in angle brackets are tags. Most come in pairs: <h1> opens a heading and </h1>, with a slash, closes it. A start tag, the content and an end tag together make an , one piece of the page. Elements sit inside other elements, like boxes inside boxes, and the spaces at the start of some lines are only there to make that easy for you to see. The browser ignores them.

Read from the top:

  • <!doctype html> tells the browser this is a modern web page.
  • <html lang="en"> holds everything else, and says the page is in English.
  • <head> holds facts about the page that aren’t shown in it. The <meta charset="utf-8"> line tells the browser how the letters in the file are stored, so that symbols such as ♥ come out right later. <title> sets the words on the browser tab.
  • <body> holds everything you can see. For now that is one <h1>, the biggest kind of heading.

Type the file in yourself rather than copying it. It takes a minute, and typing it is the fastest way to notice how it is put together. Every mark matters: a missing > or / changes what the browser sees.

§ 1.3

Save it as game.html

First make a folder for the game, for example inside your Documents folder, and call it guess-the-number. Keeping everything in one place makes the file easy to find again.

Then open your text editor, type the starter file, and save it into that folder as game.html. That is the whole job: a file of plain text whose name ends in .html, which tells your computer it is a web page. Every editor saves a little differently, so instead of click-by-click steps, here are the traps to watch for.

Plain text, not a document. Word processors such as Word, Pages or Google Docs store fonts and layout along with your words, and code can’t use them. Use a text editor.

On Windows, watch the ending. Notepad adds .txt to the name unless you set Save as type to All files before you save. Windows also hides the ending of file names, called the extension, so a file that looks like game.html can secretly be game.html.txt. To see the real name, turn on File name extensions in File Explorer’s View menu (in Windows 11 it is under Show). If the file turns out to be game.html.txt, rename it and delete the .txt. If Windows asks whether you are sure you want to change the extension, say yes.

On a Mac, make TextEdit plain. TextEdit starts new documents as rich text, with fonts and styles. Before you type, choose Format, then Make Plain Text. When you save, if TextEdit asks which extension to use, click Use .html.

Two TextEdit settings save trouble later. Both are under TextEdit, then Settings. On the New Document tab, keep Smart quotes and dashes in rich text documents only ticked, so your straight quotes “ never turn into curly ones like “ and ”, which look nicer in a letter but break code. On the Open and Save tab, tick Display HTML files as HTML code instead of formatted text, so game.html opens as the code you typed instead of as a finished page.

Keep the folder and the name exactly as they are from now on. Every part of the series changes this same file, a few lines at a time, so you never start again from scratch.

§ 1.4

Optional: an editor made for code

Notepad and TextEdit are enough for the whole series. But if you’re happy to install one program, an editor made for writing code makes the work easier. It colours the different parts of your code, so a missing quote or bracket stands out. It numbers the lines, so when an error message points at line 15, you can go straight there. It only ever saves plain text, so the traps above can’t catch you. And it finishes some things for you, such as closing tags and brackets.

A good choice is Visual Studio Code, usually called VS Code. It is free, runs on Windows, Mac and Linux, and is the editor MDN’s beginners’ course uses. Others work just as well, such as Notepad++ on Windows or Sublime Text, which is paid. If you already have a favourite, keep it.

To start with VS Code:

  1. Download it from code.visualstudio.com and install it.
  2. Choose File, then Open Folder, and pick your guess-the-number folder. If VS Code asks whether you trust the authors of the files in it, say yes: the author is you.
  3. The files in your folder are listed on the left. If game.html isn’t there yet, click the New File button beside the folder’s name, type game.html and press Enter. Click a file’s name to open it.
  4. Type, then save with Ctrl+S, or Command-S on a Mac. A dot on the file’s tab means it has changes you haven’t saved yet.

VS Code closes tags for you: as soon as you finish typing a start tag such as <h1>, it adds </h1> after it. That saves typing, but check you haven’t ended up with the end tag twice. It also pops up suggestions as you type; press Escape to close one you don’t want.

§ 1.5 · interactive

Open it, change it, refresh

Find game.html in your folder and double-click it. On most computers it opens in your web browser. If it opens in your text editor instead, right-click it (or Control-click on a Mac), choose Open With and pick your browser. You should see a page with one big heading, Guess the Number, and the same words on the browser tab.

Now keep both windows open, the editor and the browser, and try the rhythm you will use in every part of this series:

  1. Edit. Change something in the editor, for example the words between <h1> and </h1>.
  2. Save. Choose File, then Save, or press Ctrl+S on Windows, Command-S on a Mac.
  3. Refresh. Click the browser’s reload button, or press F5 or Ctrl+R on Windows, Command-R on a Mac.

The browser shows the file as it was when you last saved it, at the moment you last refreshed. Skip either step and you see the old version. Try it here first:

Try it

Edit, save, refresh

Change the heading in the editor. The browser only shows it once you have saved the file and then refreshed the page. Try refreshing before you save, too.

game.html
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Guess the Number</title>
</head>
<body>
</body>
</html>
game.html
Guess the Number
  1. 1 Edit
  2. 2 Save
  3. 3 Refresh

Change the heading in the editor, then save, then refresh.

Ctrl+S (or Command-S) inside the box also saves. Nothing you type here is run as code; it is only shown as text.

What you should see

Your browser shows Guess the Number as a big heading, and the tab says Guess the Number too. When you change the heading, save and refresh, the page changes.

§ 1.6

Where error messages appear

Browsers come with a set of tools for people who build web pages. You need only one of them in this series, and only for one job: reading . When something in your code goes wrong, the browser writes a note about it in the , saying what kind of problem it found and on which line.

Browser Windows Mac
Chrome and Edge Ctrl+Shift+J Command-Option-J
Firefox Ctrl+Shift+K Command-Option-K
Safari Command-Option-C

In Chrome and Edge, F12 also opens the developer tools, at whichever panel you used last; if that isn’t the console, click the Console tab. Safari hides its tools until you turn them on: choose Safari, then Settings, click Advanced and tick Show features for web developers.

Open the console on your game.html now. It should be empty, or show only messages that have nothing to do with your page. That is what a page without mistakes looks like. In part 2 you will cause your first error on purpose and read what it says.

Every programmer sees error messages every day. They aren’t a telling-off: they are the browser telling you exactly where to look.

§ 1.7

Your challenge

Each part ends with a small task to try on your own. Use the same rhythm every time: edit, save, refresh.

This time, make two changes to game.html. First, change the words on the browser tab to something of your own, without changing the heading on the page. Second, add a line of ordinary text under the heading, for example “A game by Alex”, using a paragraph element, which is written <p> and </p>.

If the tab doesn’t change, check which element you edited: the tab and the heading come from two different places in the file. Remember the rule from the starter file: facts about the page go in the head, and everything you can see goes in the body. Try it on your own first; the solution is there if you need it, and peeking is allowed.

Show a solution

The tab text comes from <title> in the head; the paragraph goes in the body, under the heading.

game.html
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Alex's Number Game</title>
</head>
<body>
<h1>Guess the Number</h1>
<p>A game by Alex</p>
</body>
</html>

Before part 2, change the title back to Guess the Number and delete the paragraph, so your file matches the next part.

You have a page, a place to read error messages and the rhythm to change both. In part 2 the page gets a button, and you write your first line of JavaScript to make it answer when you click it.

Sources for this part

Tools

VS Code

HTML

The console

Browsers and the web

Series Guess the Number: Your First Program
Menu

Gears & Signals · Static site · No trackers