In this video, you followed the undefined run in Ruth’s season log back from the line that crashed to the line where it started, and fixed it there.
Key Points
- The crash line is where a bad value was used. In the season log, that’s line 2 of
report.js, insideformatRun(), the topatline of the stack trace. - A guard on the crash line, such as
if (run === undefined) return "";, makes the crash go away. However, the loop still callsformatRun()one extra time, and the season log prints an empty line after the six runs with no error at all. - Instead of patching the crash line, ask why the value was bad.
formatRun()didn’t makeundefined. It came in as the argument for itsrunparameter, from its caller. - The next
atline down,printSeason()at line 8, passesruns[i]. The season has six runs, at indexes 0 to 5, but the loop’s conditioni <= runs.lengthletsireach 6, andruns[6]isundefined. - The trace’s
printSeason()line points at line 8, where the caller passed the value in. Reading that caller’s code shows where the bad index comes from: the condition on line 7. Line 7 is the bug line, where the bad value starts. Changing<=to<fixes it there, andformatRun()needs no guard. - Make it a two-step habit: crash line, then bug line. Read the report from the top to find where the bad value was used. Then follow the trace down to the caller that passed the value in, and read its code for where the bad value starts.
Final Code
This is report.js after the fix, with the loop stopping at the last run and no guard in formatRun():
report.js
export function formatRun(run) {
const who = `${run.runner} (${run.date})`;
return `${who}: ${run.distance} km in ${run.minutes} min`;
}
export function printSeason(runs) {
for (let i = 0; i < runs.length; i++) {
console.log(formatRun(runs[i]));
}
}
The other files in Ruth’s season-log folder haven’t changed in this video:
This package.json tells Node.js to treat the .js files as ES modules:
package.json
{ "type": "module" }
The season’s six runs live in runs.json:
runs.json
[
{ "id": 1, "runner": "Ruth Abebe", "date": "2026-04-04",
"distance": 5, "minutes": 27 },
{ "id": 2, "runner": "Priya Nair", "date": "2026-04-04",
"distance": 5, "minutes": 31 },
{ "id": 3, "runner": "Tomás Herrera", "date": "2026-04-04",
"distance": 8, "minutes": 44 },
{ "id": 4, "runner": "Grace Kim", "date": "2026-04-11",
"distance": 10, "minutes": 52 },
{ "id": 5, "runner": "Kwame Mensah", "date": "2026-04-11",
"distance": 5, "minutes": 24 },
{ "id": 6, "runner": "Ruth Abebe", "date": "2026-04-18",
"distance": 8, "minutes": 45 }
]
The loader in load.js reads and parses that file:
load.js
import { readFileSync } from "node:fs";
export function loadRuns(path) {
const text = readFileSync(path, "utf-8");
return JSON.parse(text);
}
Finally, main.js loads the runs and prints the season:
main.js
import { loadRuns } from "./load.js";
import { printSeason } from "./report.js";
const runs = loadRuns("runs.json");
printSeason(runs);
Run main.js again from the season-log folder, and all six runs print with no crash and no empty line:
$ node main.js
Ruth Abebe (2026-04-04): 5 km in 27 min
Priya Nair (2026-04-04): 5 km in 31 min
Tomás Herrera (2026-04-04): 8 km in 44 min
Grace Kim (2026-04-11): 10 km in 52 min
Kwame Mensah (2026-04-11): 5 km in 24 min
Ruth Abebe (2026-04-18): 8 km in 45 min
An extra pass isn’t the only thing that can go wrong in the season log. In the next lesson, Recognize the Common Runtime Errors, you’ll meet the other errors it can throw.