Skip to content

Exercise 8 of 10 · Arrays

The Number 12 Bus

What you will make

A bus route card with one line for every stop in the array, and a footer that counts the stops without being told how many there are.

The one new idea: an array type such as string[] holds several values of one type, and for...of walks through them

Most data arrives in bunches: the songs in a playlist, the rows of an order, the stops on a route. An array keeps a bunch under one name, its type says what every item in it is, and a for...of loop does the same job for each item however many there turn out to be.

Go straight to the code ↓

Several values under one name

Every name so far has held one value. A bus route is several stops, in order, that belong together. An array holds them under one name:

TypeScript
const colours: string[] = ["red", "amber", "green"];

console.log(colours);
console.log(colours.length);
Output
[ 'red', 'amber', 'green' ]
3

Square brackets make the array, and commas keep the values apart. The type string[] reads as an array of strings: square brackets after a type mean several of these, so number[] would be an array of numbers.

Printing a whole array shows its brackets and puts each piece of text in single quotes, so you can see where each value starts and stops. .length is how many values the array holds. It is something the array already knows, so there are no round brackets after it.

Every value must match

The type is a promise about every value in the array, not only the first. Slip in something else and the compiler points straight at it:

TypeScript
const colours: string[] = ["red", 2, "green"];

console.log(colours);
Output
main.ts(1,35): error TS2322: Type 'number' is not assignable to type 'string'.

for...of hands you each value

TypeScript
const colours: string[] = ["red", "amber", "green"];

for (const colour of colours) {
  console.log(`Light: ${colour.padEnd(6)}|`);
}

console.log("Sequence done.");
Output
Light: red   |
Light: amber |
Light: green |
Sequence done.

for (const colour of colours) means: for each value in colours, in order, put it in a fresh const called colour and run the block in the braces. Three values, three passes. The line after the closing brace runs once, after the loop has finished.

The compiler follows the types through the loop. Because colours is a string[], it knows colour is a string on every pass, which is why padEnd is allowed on it. You choose the name between const and of; a singular name for the one value and a plural for the array is the usual habit.

Your turn

Press Run first. The route card prints three identical lines reading o stop, because the line inside the loop has that word in double quotes.

  1. Rewrite that line as a template literal, so each stop's own name appears after the o.
  2. Add "Mill Road" and "Quayside" to the end of the array, each with a comma in front of it.

The footer needs nothing from you. It asks the array for its length, so it reports 5 by itself once the new stops are in.

If something goes wrong

If every line reads o ${stop}, dollar sign and braces included, the line is still in double quotes.

If every line shows all the stops joined by commas, the braces hold stops, the whole array, instead of stop, the one value for this pass.

If the lines show the numbers 0 to 4 instead of names, the loop says in where it should say of. That is a real kind of loop, which is why the compiler accepts it, but it walks through the positions in the array rather than the values.

If nothing prints and the compiler reports ',' expected., a comma is missing between two stops. If it reports This expression is not callable., round brackets have been added after length.

Write your code

Runs in your browser. Press Run (or Ctrl/Cmd+Enter) and the output is checked for you.

Ctrl/Cmd+Enter to run

Press Esc then Tab to move keyboard focus out of the code editor.

Ready
Output will appear here after you run your code.

The runtime is starting in the background. You can type now — it will be ready before you are.

The answer appears here once you have run your code at least once.

Things that often go wrong here

Writing for...in instead of for...of
It is a real kind of loop, so the compiler accepts it, but in walks through the positions in the array rather than the values. Every line shows a number from 0 to 4 instead of a stop. The word of hands you the stops themselves.
Putting the plural name inside the braces
stops is the whole array, and an array dropped into text turns into all its values joined by commas, so every line shows the entire route. The singular stop is the one value the loop hands you on each pass.
Forgetting the comma between two new stops
Two pieces of text side by side with nothing between them make no sense inside an array, so the compiler stops before anything prints with ',' expected. The output panel shows only that message, which is the clue that nothing ran at all.
Asking for the length with round brackets
length is something the array already knows, not something you call, so stops.length() is refused with This expression is not callable. Take the brackets off and the count appears.
Leaving the loop line in double quotes
Double quotes make the dollar sign and braces ordinary characters, so every line prints them back at you and the lines still look identical. Only backtick quotes put the value in.

Want a blank editor instead? Open the TypeScript playground.