One line at a time
A print must be put somewhere. A table needs no choosing.
let one runs. riders and one are set. off and due are dashes.- Line 2:
let oneruns.ridersandoneare set.offanddueare dashes. - Line 3:
offfills in. - Line 4:
duefills in. It is the total. - A second call, one rider alone:
dueis only 6.
Slide the line below. Watch the table fill.
Loading lab…
The rule
To step through is to run one line, then stop to look.
Stepping through shows every box after every line. That is what a print on every line would show. Look first at the row that differs from what the job expects.
The job: each rider after the first pays 2 rupees less. Here is the function.
`` function groupFare(riders, stops) { let one = 2 + 2 * stops; let off = 2 * riders; let due = one * riders - off; } ``
Take the call groupFare(3, 3). The box due is the fare to pay. After line 2, one is 2 + 2 × 3 = 8. After line 3, off is 2 × 3 = 6. After line 4, due is 8 × 3 − 6 = 18.
A call with one rider gives due = 8 × 1 − 2 = 6. For one rider the job expects 8, with nothing off.
A trace table has one column for each box. It has one row for each line that ran.
A dash is not a zero
A box not made yet has no value. That is not zero. The table shows a dash. When its line runs, the dash becomes a number.
A box made inside a call is removed when the call ends.