JavaScript 07 · Visit Sky Island with loops

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.

A distress signal lights up Pip’s antenna at Sky Dock while two cloud creatures wait on a distant platform.
Mission Read a for...of loop and understand how a counting for loop reaches each array index.

Get ready

Open Complete checkpoint below. Paste the JavaScript into a blank JavaScript editor and run it.

Walk the route. Repeat a check-in for each stop. → Count each leg. Use the current index to read a cost. → Finish the total. Print the result after the loop ends.
Walk the route → Count each leg → Finish the total

Understand the JavaScript in this lesson

Language to look for for...of, for each item, counting loop, index, increment, running total.

1. Put the route and costs in matching order

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 const for both names.

const stops = ["Workshop", "Garden", "Beacon"];
const costs = [2, 3, 4];

2. Repeat an instruction for each stop

Read 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

The first part of the counting loop sets 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

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.

console.log("Total energy:", totalCost);
Follow what happens step by step
  1. Run for...of
    Pip checks in at Workshop, Garden and Beacon.Why The loop visits each stop in order.
  2. Run the counting loop
    The running total goes from 0 to 2, 5, then 9.Why Each cost is read using its index and added once.
  3. Test index 3
    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.