Debugging For Loops
The array in the program below holds the elevation change in each hour of an eight-hour hike, in meters. A negative change is an hour that went downhill. The program was meant to print the total climb of the uphill hours, the number of downhill hours, and the largest change in any one hour.
const changes = [60, 85, -40, 210, -35, -120, 150, 45];
let climb = changes[0];
let down = 0;
let steepest = 0;
for (const change of changes) {
if (change > 0) {
climb = climb + change;
steepest = change;
}
if (change < 0) {
down = down + change;
}
}
console.log(climb, down, steepest);
Output
Press Run to see what this program prints.
Note: This IDE is for experimenting with the code in this lesson. Your changes are not saved, and refreshing the page puts back the original lesson code.
The uphill hours add to 550 meters, three hours went down, and the largest change is 210. The program prints 610 -195 45 instead, and JavaScript reports no error. This lesson traces the loop to find the mistakes and repairs them. Run the program above, then, as the lesson finds each mistake, repair it in the program and run it again.
Some mistakes stop a program. Had the last line misspelled steepest as
steepst, JavaScript would stop there with a ReferenceError and name that line. This program runs to the end and names no line, which makes its mistakes harder to find.Tracing the loop
A hand trace works through the loop on paper and writes down the value of each variable when a pass ends. The first row below holds the values set before the loop.
| Pass | change | climb | down | steepest |
|---|---|---|---|---|
| before the loop | 60 | 0 | 0 | |
| 1 | 60 | 120 | 0 | 60 |
| 2 | 85 | 205 | 0 | 85 |
| 3 | -40 | 205 | -40 | 85 |
| 4 | 210 | 415 | -40 | 210 |
| 5 | -35 | 415 | -75 | 210 |
| 6 | -120 | 415 | -195 | 210 |
| 7 | 150 | 565 | -195 | 150 |
| 8 | 45 | 610 | -195 | 45 |
Three columns go wrong: climb is too high from the first row on, down turns negative at the third pass, and steepest falls at the seventh.
This loop's header is sound:
for...of visits each item once, in order, and the trace has eight passes for eight hours. In a counting loop, starting at 1, as in for (let i = 1; i < changes.length; i++), skips the first hour. Starting at 0 but testing i <= changes.length adds a ninth pass on changes[8], which JavaScript reads as undefined, with no error.The first pass
After the first pass, climb should be 60, the first hour's climb, but the table shows 120. The line
let climb = changes[0]; starts climb at 60, and the first pass adds the same 60 again. That bug is before the loop, in a starting value.A sum starts at 0, because nothing has been added before the loop. The starting lines are above the
for line and outside its braces, so each runs once.The line
let steepest = 0; has the reverse mistake.A start of 0 does no harm on this hike, since some hour climbed more than 0 meters. It's still wrong, because no hour changed by 0, and on a hike that only went down, 0 would be the value printed. The starting lines become
let climb = 0;
let down = 0;
let steepest = changes[0];
and after the first pass, climb is 60.
The counter
A count of hours can't be negative, yet the down column is negative from the third pass on. The line
down = down + change; adds each downhill change itself, so down totals the downhill meters. That bug is in the body, in the work each pass does.The repair adds 1 instead,
down = down + 1;. Now down ends at 3 of the 8 hours, one for each of the changes -40, -35, and -120.The last pass
The last hour climbed 45 meters, less than 210, so steepest should end at 210. With both repairs in place it ends at 45. A running maximum never goes down, yet a new trace shows steepest falling from 210 to 150 on the seventh pass. The line
steepest = change; is inside the braces of if (change > 0), so every hour that climbed replaced steepest, larger or not. That bug is in the body too, in which passes do the work. The repair gives that line its own test. The three if statements can then come in any order, since none reads a variable another one changes.const changes = [60, 85, -40, 210, -35, -120, 150, 45];
let climb = 0;
let down = 0;
let steepest = changes[0];
for (const change of changes) {
if (change > 0) {
climb = climb + change;
}
if (change < 0) {
down = down + 1;
}
if (change > steepest) {
steepest = change;
}
}
console.log(climb, down, steepest);
The
console.log line sits after the loop's final brace, outside it, and it runs once the eighth pass is over. It prints 550 3 210, the answers worked out at the start.