Loading video player…

Follow Calls on the Call Stack

In this lesson, you followed Marco’s buzzer-round scoring through three functions that call each other, and read an error’s “at” lines as a picture of the calls in progress.

Key Points

  • In the buzzer round, a correct answer earns two points, plus a bonus point for buzzing in under ten seconds. bonusFor() gives back one or zero, pointsFor() calls it and adds two, and scoreAnswer() calls pointsFor() and logs the team’s points.
  • Each time a function is called, JavaScript puts a note for that call on top of a pile, with the function’s name, its parameters and its variables. It only ever works on the top note. Unlike paper notes, you never add or remove these yourself. The bottom note is for your script’s top level, the code outside any function.
  • A function that calls another pauses on that line. scoreAnswer() pauses on line 14 while pointsFor() runs, and pointsFor() pauses on line 9 while bonusFor() runs.
  • Watch out: the caller doesn’t keep going while the function it called runs. The log in scoreAnswer() on line 15 waits until every note above it is gone.
  • A return peels the top note off and hands its value down. bonusFor() returns 1, and pointsFor() picks up on line 9 with bonus set to 1.
  • Watch out: a return ends only the function it’s in. The notes under it wait for their own returns.
  • The console.log() on line 15 is a call too, so it gets a brief note of its own, but the picture here leaves it out.
  • This pile is the call stack, and each note is a frame.
  • When a script stops with an error, the “at” lines under the error list the frames on the stack at that moment, top first. In this trace, the first names pointsFor() and line 9, where it broke, and the second names scoreAnswer(), paused at line 14. The third, Object.<anonymous> at line 26, is Node’s label for the top level of scores.js. The rest is Node’s own code.
  • There’s no bonusFor() in the list, because JavaScript needs answer.seconds before it can call bonusFor(), and that’s what broke. The top line shows where it broke, and the lines below show how it got there.
  • Nothing in scores.js catches this error, so no function returns. The error ends every call on the stack, and the script stops.
  • Each call puts a frame on top of the stack, and the caller waits under it. A return takes off only the top frame and hands its value down.

The Error, Read as a Stack

Marco added a line to score The Quizzards before typing their record, so buzzerRound had nothing at index two, the third spot:

Language: JavaScript
scoreAnswer(buzzerRound[2]);

Running node scores.js printed the two scores, then stopped with a TypeError on line 9. Here’s the output, with the file path shortened and Node’s own lines cut:

Language: Program Output
Know-It-Owls: 3 points
Table Six: 2 points
.../scores.js:9
  const bonus = bonusFor(answer.seconds);
                                ^

TypeError: Cannot read properties of undefined (reading 'seconds')
    at pointsFor (.../scores.js:9:33)
    at scoreAnswer (.../scores.js:14:18)
    at Object.<anonymous> (.../scores.js:26:1)
    ...

pointsFor() is on top, because that’s where it broke. scoreAnswer() is under it, paused at line 14, and the top level is at the bottom, at line 26, the line that passed in the missing record. There’s no bonusFor(), because JavaScript needed answer.seconds before it could call it, and that’s what broke.

Final Code

With The Quizzards’ record added, scores.js scores all three answers. The new record pushes the calls down a line, so the third call is now on line 27:

Language: JavaScript Filename: scores.js
function bonusFor(seconds) {
  if (seconds < 10) {
    return 1;
  }
  return 0;
}

function pointsFor(answer) {
  const bonus = bonusFor(answer.seconds);
  return 2 + bonus;
}

function scoreAnswer(answer) {
  const points = pointsFor(answer);
  console.log(`${answer.team}: ${points} points`);
  return points;
}

const buzzerRound = [
  { team: "Know-It-Owls", seconds: 8 },
  { team: "Table Six", seconds: 14 },
  { team: "The Quizzards", seconds: 11 },
];

scoreAnswer(buzzerRound[0]);
scoreAnswer(buzzerRound[1]);
scoreAnswer(buzzerRound[2]);

Run it from your terminal, and each team gets its points:

Language: Shell
$ node scores.js
Know-It-Owls: 3 points
Table Six: 2 points
The Quizzards: 2 points

Next, in Trace a Recursive Round Total, one function calls itself, and its frames pile up on the same stack.

00:00 Marco hosts the weekly quiz at the Lantern, a neighborhood pub, and keeps score with a Node script. Its functions call other functions, and when one breaks deep inside, you need a picture of the calls in progress.

00:12 All you need is functions and return values. Here’s scores.js. In the buzzer round, a correct answer earns two points, plus a bonus point for buzzing in under ten seconds.

00:24 bonusFor() gives back one or zero. pointsFor() calls it and adds two. scoreAnswer() calls pointsFor() and logs the team’s points.

00:34 The script scores both answers in buzzerRound, at the bottom. Run it, and the terminal shows three points for the Know-It-Owls and two for Table Six.

00:44 How does JavaScript get into each function, and back out with a value?

00:51 Picture a pile of sticky notes. Each time a function is called, JavaScript writes a note with that function’s name, parameters and variables, and sticks it on top of the pile.

01:01 It only ever works on the top note. Unlike paper notes, you never add or remove these yourself. Follow the Know-It-Owls’ answer. The bottom note is for the script’s top level, the code outside any function, on line 24.

01:16 Its call to scoreAnswer() lands a note on top, with answer holding the Know-It-Owls’ record. scoreAnswer() pauses on line 14 to call pointsFor(), whose note lands on top.

01:29 pointsFor() pauses on line 9 to call bonusFor() with eight, and that note lands on top. Watch out: scoreAnswer() doesn’t keep going while they run.

01:39 Its log on line 15 waits until every note above it is gone.

01:46 Eight is under ten, so bonusFor() returns one. The return peels its note off and hands the one down. pointsFor() picks up on line 9, with bonus set to one.

01:58 Watch out: that return ended bonusFor() only. The notes under it wait for their own returns.

02:06 pointsFor() returns three, so its note comes off, and scoreAnswer() gets three. Now it logs the Know-It-Owls with three points.

02:15 That log is a call too, with its own note, but the picture leaves it out. Its return takes its note off, and the top level moves on to Table Six.

02:24 This pile is the call stack, and each note is a frame.

02:30 Now Marco adds line 26 for The Quizzards before typing their record, so buzzerRound has nothing at index two, the third spot. Run the script. After two scores, it stops with a TypeError on line 9.

02:44 Reading index two gives undefined, so answer itself is undefined.

02:50 Line 9 is inside pointsFor(), on its first line. Your turn: pause the video and predict which frames are on the stack when line 9 breaks.

03:05 pointsFor() is on top, with scoreAnswer() under it and the top level at the bottom. The “at” lines under the error list them, top first.

03:14 The first names pointsFor() and line 9, where it broke. The second is scoreAnswer(), paused at line 14. The third, Object.<anonymous>, is Node’s label for the top level, at line 26.

03:28 The rest is Node’s own code, cut here. Nothing in the script catches the error, so it ends every call at once.

03:37 There’s no bonusFor(), because JavaScript needs answer’s seconds before it can call it, and that’s what broke. The top line shows where it broke, and the lines below show how it got there.

03:48 With their record added, The Quizzards get two points.

03:53 Each call puts a frame on top of the stack, and the caller waits under it. A return takes off only the top frame and hands its value down. You’ve followed three calls frame by frame, and read an error as a picture of the stack.

04:08 Next, in Trace a Recursive Round Total, one function calls itself.

Become a Member to join the conversation.