Loading video player…

Follow the Bad Value Back

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, inside formatRun(), the top at line 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 calls formatRun() 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 make undefined. It came in as the argument for its run parameter, from its caller.
  • The next at line down, printSeason() at line 8, passes runs[i]. The season has six runs, at indexes 0 to 5, but the loop’s condition i <= runs.length lets i reach 6, and runs[6] is undefined.
  • 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, and formatRun() 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():

Language: JavaScript Filename: 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:

Language: JSON Filename: package.json
{ "type": "module" }

The season’s six runs live in runs.json:

Language: JSON Filename: 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:

Language: JavaScript Filename: 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:

Language: JavaScript Filename: 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:

Language: Shell
$ 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.

00:00 Ruth’s season log prints all six runs, and then it crashes. You’ve already read this report from the top. It’s a TypeError: run was undefined when the code read .runner from it.

00:12 So formatRun() got no run at all. It was thrown in report.js, on line 2. Below that, printSeason() and main.js are the calls that led there.

00:24 You know where it crashed. The question for this video is where to fix it.

00:30 The crash is on line 2 of formatRun(), so the natural place for a fix is right there. You add a guard at the top of the function: a check that leaves early.

00:40 If run is undefined, it returns an empty string instead of reading from it. The loop and the data haven’t changed. Your turn: pause the video and predict what Ruth sees when she runs the season log again.

00:59 There’s no crash. Six runs print, then one empty line, and the program ends without an error.

01:08 That empty line is the guard at work. Six runs, then a seventh line: something called formatRun() a seventh time, with no run, and the guard turned that call into an empty string.

01:20 The crash is gone, but its cause isn’t. A crash gets your attention right away. In a longer report, nobody would notice an empty line. Checking for a missing value isn’t wrong in itself, but here it hid the cause.

01:35 So take the guard back out, and the crash is back.

01:40 Instead of patching where it crashed, ask why run was undefined. The stack trace gives you the route, one caller at a time. The top at line is the crash line.

01:52 formatRun() didn’t make that value. It received it as an argument. So the bad value came from its caller. One line down, printSeason() was paused on line 8, in the same file.

02:06 That’s the call, and it passes runs[i] as the run. So which value did i have at that moment?

02:14 Ruth’s season has six runs, so its length is 6 and its indexes go from 0 to 5. The loop starts at 0 and keeps going while i is less than or equal to the length.

02:27 Watch the passes. Indexes 0 to 5 each find a run. Then i becomes 6. The condition is still true, and index 6 is undefined, because there’s no seventh run.

02:41 The trace brought you to line 8, but line 8 only passes runs[i] along. It doesn’t decide what i is. Read the line above it. The loop’s condition lets i reach 6.

02:52 That’s the bug line, line 7, where the bad value starts. The next at line down is main.js. All six runs printed fine, so the bad value didn’t start there, and you can stop.

03:07 Change the less-than-or-equal sign to a less-than sign. Now the loop stops after index 5, the last run in the season. Run the season log again. Six runs, no crash, and no empty line.

03:23 formatRun() has no guard, and it doesn’t need one. Every call now gives it a real run.

03:31 Make it a two-step habit. First, read the report from the top to find the crash line, where the bad value was used. Then ask where that value came from. Follow the trace down to the caller that passed it in, and read that caller’s code for the bug line, where the bad value starts.

03:49 That’s where the fix goes.

03:52 You followed Ruth’s undefined run back to its loop and fixed the loop. An extra pass isn’t the only thing that can go wrong in the season log, though. In the next lesson, Recognize the Common Runtime Errors, you’ll meet the other errors it can throw.

Become a Member to join the conversation.