Skip to content
Your progress

Checking your sign-in…

Who's learning?

Each profile keeps its own read lessons, badges, streak, and practice scores on this device.

Lesson 6 of 11

One line at a time

Show on a big screen

6 min

Text size

A print must be put somewhere. A table needs no choosing.

Line 2: let one runs. riders and one are set. off and due are dashes.
Step 1 of 4
  1. Line 2: let one runs. riders and one are set. off and due are dashes.
  2. Line 3: off fills in.
  3. Line 4: due fills in. It is the total.
  4. A second call, one rider alone: due is 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.

Try the lab to finish this lesson.