JavaScript lesson 7 of 50 · Beginner · About 25 minutes
Teach Pip to report every stop and total its costs without writing the same instruction again and again.

Get ready
Open Complete checkpoint below. Paste the JavaScript into a blank JavaScript editor and run it. Language to look for for...of, for each item, counting loop, index, increment, running total. Write the stops in the order you want Pip to visit them. Add the matching travel costs in the other array, so index zero describes the Workshop in both lists. The route does not change during this program, so use Read The first part of the counting loop sets Place the total message after the closing brace. You get one final total after Pip has counted the entire route. If you place the message inside the loop, Pip gives you an updated total on each pass instead.
Understand the JavaScript in this lesson
1. Put the route and costs in matching order
const for both names.const stops = ["Workshop", "Garden", "Beacon"];
const costs = [2, 3, 4];2. Repeat an instruction for each stop
for (const stop of stops) as “for each value in stops, call the current value stop”. The loop runs the lines inside its braces once per item. First stop means Workshop, then Garden, then Beacon. JavaScript gives the loop a fresh stop name for the current turn, so const works here.for (const stop of stops) {
console.log(`Check-in: ${stop}.`);
}3. Use a counter for the leg number
index to zero. The middle part checks whether it is below the list length before each turn. The last part adds one after each turn. The two inner semicolons separate these three parts. totalCost += cost means add this leg’s cost to the total and save the result back under the same name. Pip starts its count at zero, while the printed leg number starts at one.let totalCost = 0;
for (let index = 0; index < costs.length; index += 1) {
const cost = costs[index];
totalCost += cost;
console.log(`Leg ${index + 1}: ${cost} energy.`);
}4. Print after the loop
console.log("Total energy:", totalCost);
Pip checks in at Workshop, Garden and Beacon.Why The loop visits each stop in order.
The running total goes from 0 to 2, 5, then 9.Why Each cost is read using its index and added once.
The loop stops and prints Total energy: 9.Why Three is not less than costs.length, so there is no extra turn.
Predict before you run What if the counting loop starts with index = 1?
Reveal the answer
It skips the cost at index zero. The final total becomes 7, and the first printed leg number is 2.
Complete checkpoint
JavaScript · the instructions
const stops = ["Workshop", "Garden", "Beacon"];
const costs = [2, 3, 4];
for (const stop of stops) {
console.log(`Check-in: ${stop}.`);
}
let totalCost = 0;
for (let index = 0; index < costs.length; index += 1) {
const cost = costs[index];
totalCost += cost;
console.log(`Leg ${index + 1}: ${cost} energy.`);
}
console.log("Total energy:", totalCost);
Read the JavaScript line by line
Open a small group of lines while you read the complete checkpoint. Blank lines separate ideas. Closing braces mark the end of a block. An ellipsis here means you can read the rest in the full program.
Lines 1–15
-
Line 1
const stops = ["Workshop", "Garden", "Beacon"]; - Set up stops. Keep this name attached to the same value. Square brackets create an ordered array. Its first position is 0.
-
Line 2
const costs = [2, 3, 4]; - Set up costs. Keep this name attached to the same value. Square brackets create an ordered array. Its first position is 0.
-
Line 4
for (const stop of stops) { - Take one value at a time from this sequence and run the body with it.
-
Line 5
console.log(`Check-in: ${stop}.`); - Show a message or value in the output. Use it to watch the game work.
-
Line 8
let totalCost = 0; - Set up totalCost. Give this name a starting value that later code can change.
-
Line 9
for (let index = 0; index < costs.length; index += 1) { - Set up the counter, check the condition and run the body. Update the counter after each pass.
-
Line 10
const cost = costs[index]; - Set up cost. Keep this name attached to the same value. Work out what is inside the brackets first, then use it as the key or index.
-
Line 11
totalCost += cost; - Update the existing value with the operation shown.
-
Line 12
console.log(`Leg ${index + 1}: ${cost} energy.`); - Show a message or value in the output. Use it to watch the game work.
-
Line 15
console.log("Total energy:", totalCost); - Show a message or value in the output. Use it to watch the game work.
Mission challenge
Insert the Supply Dock between the Garden and Beacon. Add another cost at the matching position and predict the final total.
Change one piece at a time. Run the checkpoint again and compare what you expected with what actually happened.