Looping With while and do...while

In the last exercise, you numbered the stops along each route. Now the report needs to know what happened at them. Its list of late departures starts with the N3 that left Central Station at 23:40, eleven minutes behind schedule. Amira was driving, and the operations team wants to know where she caught up, because the timetable builds a few minutes of slack into some stops.

The Python script answers that with a while loop, which runs until something changes rather than once per item or number. Almost all of it carries over to JavaScript, which is exactly why the few spots where your Python habits misfire can slip past you. Start with the loop itself.

Loop Until the Bus Catches Up

The Python script keeps N3’s stops after Central Station in a list, each with its slack in minutes. Its loop subtracts each stop’s slack from the delay for as long as the bus is still late. The delay determines when the loop stops. The index is only how the loop reaches the next stop. A for...of loop with a break would work too, but while puts the stopping rule in the header, where you read it first.

Here’s the loop as it stands in the Python script, printing the delay after each stop:

Language: Python
stops = [
    {"name": "Market Square", "slack": 3},
    {"name": "Elm Street", "slack": 0},
    {"name": "Riverside", "slack": 4},
    {"name": "Hospital", "slack": 4},
    {"name": "Northgate", "slack": 2},
]
late = 11
stop_index = 0
while late > 0 and stop_index < len(stops):
    late -= stops[stop_index]["slack"]
    print(f"{stops[stop_index]['name']}: {late} min late")
    stop_index += 1

The JavaScript port follows those steps in order, so it prints the same report:

Language: JavaScript
const stops = [
  { name: "Market Square", slack: 3 },
  { name: "Elm Street", slack: 0 },
  { name: "Riverside", slack: 4 },
  { name: "Hospital", slack: 4 },
  { name: "Northgate", slack: 2 },
];
let late = 11;
let stopIndex = 0;
while (late > 0 && stopIndex < stops.length) {
  late -= stops[stopIndex].slack;
  console.log(`${stops[stopIndex].name}: ${late} min late`);
  stopIndex++;
}

Run it, and the delay shrinks stop by stop:

Language: Program Output
Market Square: 8 min late
Elm Street: 8 min late
Riverside: 4 min late
Hospital: 0 min late

Amira was back on schedule at the Hospital stop, so the loop stopped there, and Northgate never made it into the report. If a stop’s slack had pushed the delay below zero, late > 0 would have stopped the loop all the same. The second half of the condition is the fallback. It stops the loop at the end of the line if a bus never catches up, and it’s joined to the first half with &&, which is how JavaScript spells Python’s and.

Locked learning resources

Join us and get access to thousands of tutorials and a community of expert JavaScript developers.

Unlock This Lesson

Already a member? Sign-In

Locked learning resources

The full lesson is for members only. Join us and get access to thousands of tutorials and a community of expert JavaScript developers.

Unlock This Lesson

Already a member? Sign-In

Become a Member to join the conversation.