JavaScript 103: Beat Builder. A glowing drum machine with orange and green pads and a bright playhead
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 3 of 5

Lists

Eight short ideas about keeping many values in one place. Then you build Beat Builder: a drum machine where every track is a list you wrote - and then play along to it as a rhythm game.

START HEREWhy lists

So far every variable held one value. A game needs more: every enemy on screen, every card in a hand, every step in a song. Making enemy1, enemy2, enemy3... falls apart the moment there are fifty of them, or when you don't know how many there will be.

A list keeps any number of values, in order, under one name. And because Primer 2 gave you loops, you can do something to every one of them with three lines of code.

Same rules as before. Predict, run, change. Red lines tell you where JavaScript got confused.

JavaScript calls a list an array. You will see both words. They mean the same thing here.

CONCEPT 1A list holds many values

Write a list with square brackets, and separate the items with commas. Any kind of value can go in: text, numbers, true or false.

Now change it: add a fourth colour. Then make a list of four numbers - your last four test scores, say - and print it.

The same thing in C++
std::vector<std::string> colors = {"red", "green", "blue"};
cout << colors.size();

CONCEPT 2Counting from zero

Each item has a position number, called its index. The first item is index 0, not 1. Ask for an item by putting its index in square brackets after the name.

Now change it: print green. Then think about why the last line doesn't crash.

CONCEPT 3How many, and the last one

.length tells you how many items a list has. Because counting starts at 0, the last item is always at length - 1. That works no matter how long the list gets.

Now change it: add two more scores to the list. Line 3 still prints the last one, without you changing it.

CONCEPT 4Changing one item

Put an index on the left of = to replace that one item. The rest of the list stays as it was.

Now change it: set the last item to 5 using lives.length - 1 rather than typing the number 2.

CONCEPT 5push and pop

push( ) adds an item to the end of a list. pop() takes the last item off. The list grows and shrinks as you go, which is why you often start with an empty list [] and push into it from a loop.

Now change it: start with an empty list [] and use a for loop to push the numbers 1 to 5 into it. Print the list.

CONCEPT 6Looping over a list

This is where Primer 2 pays off. Loop i from 0 while i < list.length, and use list[i] inside. It visits every item exactly once, whatever the list's size.

Now break it: change < to <= and run it. That extra line is Primer 2's off-by-one, and it is the most common list bug there is.

CONCEPT 7for...of, and counting what you find

When you need each value but not its index, for (const s of scores) hands you one item per round, with no counter to get wrong. Add an if and a running total, and you can count or search through any list.

Now change it: find the highest score instead. Start with let best = 0;, and inside the loop replace best whenever s is bigger.

CONCEPT 8A list is shared, not copied

In Primer 1, let b = a; copied the number in a. Lists don't work that way. const b = a; gives the same list a second name. Change it through either name, and both see the change.

Now fix it: make b a real copy, so a stays [1, 2]. Start b as an empty list, and push every item of a into it with a for...of loop.

THE BUILDBeat Builder

You are programming a drum machine. One bar of music is 16 steps. Every track - kick, snare, hi-hat, clap and bass - is a list of 16 steps that you build with code.

Then you play it. Play along turns your kick and snare into a rhythm game: press J on every kick and F on every snare, in time, for a Perfect, a Good or a Miss. The beat you write is the level you play.

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

RUN loads your tracks. Play plays them on a loop. CHECK CODE tests every step. Turn your sound on.

beat.js - your code
beat-builder.html
112 bpm
FJ

F = snare, J = kick. Press Play along, then hit the keys in time. One bar to count in, then four bars to score.

Console