Skip to content
Exit presentation

Computational Thinking III: Functions & Debugging

One line at a time

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.

Jump to lesson

This track

  1. 0 Start here
  2. 1 A name for a group of steps
  3. 2 What you hand it, and what each end is called
  4. 3 The value it hands back
  5. 4 One call inside another
  6. 5 A print in the middle
  7. 6 One line at a time
  8. 7 Predict, then run
  9. 8 Halve the hunt
  10. 9 The big picture
  11. 10 Track quiz

Other tracks