JavaScript 102: Block Hopper. A blocky hero leaps a gap between glowing platforms, past coins and spikes, toward a green flag
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 2 of 5

Loops

Eight short ideas about doing something over and over without typing it over and over. Then you build a platformer level, Block Hopper, where every floor, stair and coin comes from a loop you wrote.

START HEREWhy loops

In Primer 1, every line ran once. That is fine for a vault with one screen. It is hopeless for a level with 24 floor tiles, or a game that checks every enemy sixty times a second.

A loop runs the same lines again and again, changing one thing each time. It is the difference between typing block(0, 0); twenty-four times and typing it once.

Without a loop
console.log(1);
console.log(2);
console.log(3);
console.log(4);
console.log(5);
With a loop
for (let i = 1; i <= 5; i++) {
  console.log(i);
}

Both print 1 to 5. Only one of them still works when you need 1 to 500.

Same rules as Primer 1. Predict before you run. Change the code when the page asks. Red lines tell you where JavaScript got confused.

New this time: if you write a loop that never stops, this page stops it for you after 100,000 rounds and marks the loop in red. In a normal web page, that loop would freeze the whole tab.

CONCEPT 1The for loop

A for loop has three parts inside its round brackets, separated by semicolons:

  1. Start: let i = 1 - make a counter and give it a first value.
  2. Keep going while: i <= 5 - a true-or-false question asked before every round. When it is false, the loop ends.
  3. Step: i++ - what happens after every round. i++ is short for i = i + 1.

The code inside the curly braces is the body. It runs once per round.

Now change it: make it print 1 to 10. Then make it print Round 1, Round 2... by joining text onto i, as you did in Primer 1.

The same thing in C++
for (int i = 1; i <= 5; i++) {
  cout << i;
}

CONCEPT 2Counting from zero

Most loops you will read start at 0, not 1, and use < rather than <=. for (let i = 0; i < 3; i++) runs 3 times: with i as 0, 1 and 2.

The habit comes from lists, where the first item is number 0 (Primer 3). It is also handy: the number after < is exactly how many rounds you get.

Now change it: make it run 24 times - the width of the level you will build. What is the last number it prints?

CONCEPT 3Counting by twos, and counting down

The step doesn't have to be i++. i += 2 adds 2 each round (it is short for i = i + 2). i-- takes 1 away, so the loop counts down - then the "keep going" question has to flip too, to > or >=.

Now change it: make the first loop print 10, 20, 30, 40, 50. Then make the second print a countdown from 10 that ends with Liftoff! (that last line goes after the loop, not inside it).

CONCEPT 4while: keep going as long as...

A while loop has only the question. You make the counter before it, and you move the counter yourself, inside the body.

Use for when you know how many rounds you want. Use while when you only know when to stop - "while the player has lives", "while the enemy is still alive".

Now change it: add console.log("done, n is " + n); after the loop. Predict what n is once the loop has finished.

The same thing in C++
int n = 3;
while (n > 0) {
  cout << n;
  n = n - 1;
}

CONCEPT 5The loop that never ends

If nothing inside a loop moves it toward its end, the question never becomes false, and the loop runs forever. It is the classic loop bug, and every programmer writes one sooner or later.

Now fix it: add one line inside the loop so it prints 0 to 4 and stops.

CONCEPT 6Running totals

To add up as you go, make a variable before the loop and add to it inside the loop. This is Primer 1's score = score + 2, done once per round.

Now change it: move console.log(total); inside the loop, and run it again. Then move let total = 0; inside the loop too, and see what breaks.

CONCEPT 7An if inside a loop

A loop goes through every value. An if inside it picks out the ones you care about. Each round asks the question again, with that round's value.

Now change it: print every number from 1 to 10 except 4 and 5. You will need || or && from Primer 1 - this is exactly how you will dig the pit in the game.

CONCEPT 8Off by one

The commonest loop mistake isn't a loop that never ends. It is a loop that runs one time too many, or one too few. It is almost always the difference between < and <=, or starting at 0 versus 1.

When a loop is nearly right, check the first round and the last round. The middle usually takes care of itself.

Now change it: make it print Tile 1 to Tile 5. There are two different one-character fixes. Find both.

THE BUILDBlock Hopper

You are building a platformer level - a floor with a pit, a staircase, a row of coins, spikes and a finish flag - and then playing it. Every piece comes from a loop you type.

This time your code runs once, when you press RUN, and builds the level. The game on the right - the player, the jumping, the gravity - is already built. Click the level to play it.

Same rules as Vault Cracker. Type it all by hand: paste is off. No AI. Your code saves in this browser as you type.

RUN builds the level. CHECK CODE tests your code against every step. Arrow keys or A / D move, and Up, W or Space jumps. A jump goes two tiles up.

level.js - your code
block-hopper.html
Console