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, andscoreAnswer()callspointsFor()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 whilepointsFor()runs, andpointsFor()pauses on line 9 whilebonusFor()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
returnpeels the top note off and hands its value down.bonusFor()returns1, andpointsFor()picks up on line 9 withbonusset to1. - 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 namesscoreAnswer(), paused at line 14. The third,Object.<anonymous>at line 26, is Node’s label for the top level ofscores.js. The rest is Node’s own code. - There’s no
bonusFor()in the list, because JavaScript needsanswer.secondsbefore it can callbonusFor(), and that’s what broke. The top line shows where it broke, and the lines below show how it got there. - Nothing in
scores.jscatches 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:
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:
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:
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:
$ 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.