JavaScript 101: Vault Cracker. A glowing vault door, open a crack, with curly braces floating in the light
Course image created with generative AI using OpenAI tools, with direction and curation by the course instructor.

CTEC 200 · Learn to Code by Hand · Primer 1 of 5

Values and Decisions

Eight short ideas, each one tried out right here on this page. Then you build a working game, Vault Cracker, typing every line yourself.

START HEREWhy JavaScript first

You asked to learn to write code yourself, without Claude doing the typing. JavaScript is the first language for four reasons.

JavaScript
let lives = 3;
if (lives > 0) {
  console.log("Keep playing");
}
C++
int lives = 3;
if (lives > 0) {
  cout << "Keep playing";
}

The only real difference above is how you create the variable and how you print. The decision itself is identical.

How this page works. Each idea has a short example. Before you press Run, pick what you think it will print - Run stays locked until you do. Guessing wrong is fine; it shows you exactly what to fix in your thinking. Then change the code as the page suggests and run it again.

The examples are yours to break. Reset puts them back.

Red lines are normal. When JavaScript can't follow your code, the line turns red with a note under it. The real mistake is often at the end of the line above.

CONCEPT 1Code runs top to bottom

A program is a list of instructions, called statements. JavaScript runs them one at a time, from the top, in order. Most end with a semicolon ;.

console.log( ) prints whatever is inside the brackets to the console - the dark box under each example. Text goes inside quotes.

Now change it: add a fourth line that prints your name. Then move that line to the very top and run it again.

CONCEPT 2Variables hold values

A variable is a named box with a value in it. You create one with let or const, a name, =, and the value.

Read = as "store", not "equals". let lives = 3; means "make a box called lives and store 3 in it".

Names can't have spaces, so join words and capitalise each new word after the first: playerName, maxTries. Capitals count - playername is a different box.

Now change it: add a line at the bottom that says playerName = "Kai"; and run it. Read the red note. Then change const to let on line 2 and run again.

The same thing in C++
int lives = 3;                         // C++ wants the type: int is a whole number
const std::string playerName = "Rin";  // string is text
cout << playerName;

CONCEPT 3Changing a value

To change a let variable, store a new value in it - with no let this time, because the box already exists.

JavaScript works out the right side first, using what is in the box right now, and then stores the answer back in the box.

Now change it: add score = score * 10; just before the console.log. Predict the answer in your head before you run it.

CONCEPT 4A variable is a snapshot, not a link

When you write let b = a;, JavaScript copies the value that is in a at that moment into b. The two boxes are not connected. Changing a later does nothing to b.

Now change it: add console.log(a); at the end. Predict both lines before you run it.

CONCEPT 5Text and numbers are different things

Anything in quotes is a string - text. "42" is a string. 42 is a number. They look alike, but JavaScript treats them differently.

+ adds numbers. But if either side is a string, + joins them into longer text. It works left to right, one + at a time.

Now change it: on line 3, put brackets round (3 + 4) and run it. Brackets go first, just like in maths.

CONCEPT 6Questions with a true or false answer

A comparison asks a question, and the answer is always true or false.

One = stores a value. Three, ===, compares two values. Mixing them up is the most common beginner bug there is, so this page flags it for you.

Now change it: change the last line's && to ||, then set lives to 0. Predict each run first.

CONCEPT 7if / else: choose one path

An if asks a true-or-false question in round brackets. When the answer is true, the code inside its curly braces { } runs. When it is false, the else block runs. Only one of the two ever runs.

You can leave out the else. Then, when the answer is false, nothing happens and JavaScript carries on with the next line.

Now change it: make it print Hot without touching lines 2 to 6. Then try writing if (temp = 90) and read the red note.

The same thing in C++
int temp = 75;
if (temp > 80) {
  cout << "Hot";
} else {
  cout << "Nice";
}

CONCEPT 8else if: the first match wins

To choose between more than two paths, chain them with else if. JavaScript checks each question from the top and stops at the first one that is true. It never looks at the rest.

So the order is up to you. Put the most specific checks first.

Now change it: rearrange the checks so that 95 prints Top marks and 60 still prints Pass.

THE BUILDVault Cracker

You are writing the brain of a safe. The vault picks a secret code from 1 to 99, and the player gets five guesses to crack it. After each guess, your code decides what the vault says.

The vault on the right is already built - its screen, lamp and door. Everything it thinks is yours to type. There are seven steps. Each one adds a few lines and uses ideas from above.

Rules of the build. Type it all by hand: paste is switched off in this editor. No AI. If you get stuck, scroll up to the concept the step points to. Your code saves in this browser as you type.

RUN loads your code into a fresh vault. Then type guesses on the right and press CRACK. CHECK CODE tests your code against every step and tells you what is missing.

vault.js - your code
vault-cracker.html
- - -
GOLD
(you did it)

What your code was handed

Guesses

    Console