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.
const means the name colors always points to this same list. The list itself can still change - you can add to it and swap items, as you will in a moment.
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.
Three of you picked "Nothing - lists start at 1" on the Hand-Coding Check, so this is the big one. Three items means indexes 0, 1 and 2. Asking for index 3 gives undefined - JavaScript's word for "there's nothing there".
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.
Nobody got the length question on the Check. Three items, so length is 3, and the last index is 3 - 1, which is 2.
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.
Index 1 is the second item. Think of three players: player 0, player 1 and player 2. Player 1 just lost all their lives.
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.
Nobody got push on the Check either. After the push the list is ["a", "b"], so its length is 2. Then pop() removes "b" again.
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.
Nobody spotted the off-by-one on the Check. With <=, the loop also runs with i as 3, and names[3] doesn't exist.
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.
Round by round, s is 40, then 75, then 90, then 30. Only 75 and 90 are over 50.
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.
This is the promise from Primer 1, Concept 4. Numbers and text are copied. Lists are shared, because copying a list of a million items every time you passed it around would be slow. The last step of the game is built around this exact trap.
