Tracing Code with a Trace Table — JavaScript

Tracing means running code in your head, line by line, before the computer runs it. Write down the values of the variables in a trace table: one column per…

Before you run code, run it in your head. Make a table with one column per variable and one row per step. You write down the value of each variable step by step, instead of guessing the result.

Running code in your head, line by line, is called tracing. The table where you write down the values of the variables is called a trace table. Tracing is useful for two things. First, to check your guess before you press Run. Second, to find a bug (a mistake in the code) that shows no error message but makes the result wrong.

How to fill in a trace table

For a loop, fill in the table with these four rules:

  1. Make one column per variable, one column per condition, and one Output column for what console.log prints.
  2. The first table row holds the values of the variables before the loop starts.
  3. Each pass gets one table row. Start with the condition. Write it with the variable's value at that moment, then its result. For example, the condition day <= 3 when day is 1 is written 1 <= 3: true. If it is true, run the code inside { } in your head, line by line, and write the new value of each variable that changes. If a variable's value does not change, its cell (a box in the table) stays empty.
  4. If the loop condition is false, the loop stops. Write down that false check in a row too, then add one table row for the code after the loop. The last table row is the answer.

A short loop: Sari's savings

Sari saves money for three days. On the first day she saves $10, and each day after that she saves $5 more. This program calculates her savings. Before you read the table, guess first: does it print 45 or 60?

let savings = 0;
let deposit = 10;

for (let day = 1; day <= 3; day++) {
  savings += deposit;
  deposit += 5;
}

console.log(savings);

This is its trace table:

Step day day <= 3 savings deposit Output
Before the loop 0 10
Pass 1 1 1 <= 3: true 10 15
Pass 2 2 2 <= 3: true 25 20
Pass 3 3 3 <= 3: true 45 25
Check 4 4 4 <= 3: false
After the loop 45

It prints 45. On each pass, savings += deposit runs first, and only then deposit += 5. So the deposits that go into the savings are 10, 15 and 20. If you swap those two lines of code, the deposit increases before it is saved, and the result is 60. When you only guess, an order like this is easy to miss.

Look at the day column too. let day = 1 runs once, right before the first check, so the day column starts in pass 1. day++ runs at the end of each pass, right before the next check. So the new value of day is written in the next table row: 2 in pass 2, 3 in pass 3, then 4 in check 4. In check 4, 4 <= 3 is false, and the loop stops.

A loop with an if inside

Budi's usual internet cafe charges $5 per hour for the first two hours, then $3 per hour after that. Budi plays for 4 hours. This program calculates the cost:

let cost = 0;

for (let hour = 1; hour <= 4; hour++) {
  if (hour <= 2) {
    cost += 5;
  } else {
    cost += 3;
  }
}

console.log("To pay: " + cost);

Now there are two conditions: the loop condition (hour <= 4) and the if condition (hour <= 2). Each gets its own column:

Step hour hour <= 4 hour <= 2 cost Output
Before the loop 0
Pass 1 1 1 <= 4: true 1 <= 2: true 5
Pass 2 2 2 <= 4: true 2 <= 2: true 10
Pass 3 3 3 <= 4: true 3 <= 2: false 13
Pass 4 4 4 <= 4: true 4 <= 2: false 16
Check 5 5 5 <= 4: false
After the loop To pay: 16

The hour <= 2 column shows which part runs. If it is true, the if part adds 5. If it is false, the else part adds 3. Look at pass 2: 2 <= 2 is still true, because 2 is equal to 2. The last table row is the answer: To pay: 16.

One pass off

Suppose the loop condition is written hour < 4. The program runs with no error, but prints To pay: 13. If you trace it, the trace table shows the cause: at check 4, 4 < 4 is false. The loop stops before the fourth hour is counted.

A bug like this is called off-by-one: the loop runs one pass too few, or one pass too many. The cause is almost always < and <= swapped, or a starting number of 0 and 1 swapped. If the result is wrong by exactly what one pass adds (here $3, the cost of the fourth hour), trace the last check first.

The computer can write the trace too

For a long loop, writing the table by hand is tiring. Add a temporary console.log at the end of the code inside the loop:

let cost = 0;

for (let hour = 1; hour <= 4; hour++) {
  if (hour <= 2) {
    cost += 5;
  } else {
    cost += 3;
  }
  console.log(hour, cost);
}

console.log("To pay: " + cost);

The output:

1 5
2 10
3 13
4 16
To pay: 16

Each pass prints one line of output, the same as the hour and cost columns in the internet cafe's trace table above. Delete that extra console.log again once you have finished tracing.

TL;DR: Tracing means running code in your head, line by line, before the computer runs it. Write down the values of the variables in a trace table: one column per variable, one column per condition, and one Output column. One pass of the loop becomes one table row. At the start of each pass, write the condition with the variables' values at that moment (for example 1 <= 3), then its result (true or false). After the code inside the loop runs, write the new value of each variable that changed. If the loop condition is false, the loop stops, and the last table row is the answer. A trace table helps you find a bug (a mistake in the code) that shows no error. One example is off-by-one: a loop that runs one pass too few or too many, for example because of a < that should be <=. For a long loop, a temporary console.log inside the loop can record the values for you.

⚡ Run it in your head first

Write:

let savings = 0;
let deposit = 10;

for (let day = 1; day <= 3; day++) {
  savings += deposit;
  deposit += 5;
}

console.log(savings);
Guess first: deposit starts at 10 and increases by 5 on each pass. Does it print 45 or 60?

It prints 45. On each pass, savings += deposit runs before deposit += 5. So the amounts saved are 10, 15 and 20.

Meaning: Tracing means running code in your head, line by line, and writing down the values of the variables in a trace table. A trace table has one column per variable, one column per condition, and one output column. One pass of the loop becomes one table row. The last table row is the answer.

⚡ Write down the result of each condition

Write:

let cost = 0;

for (let hour = 1; hour <= 4; hour++) {
  if (hour <= 2) {
    cost += 5;
  } else {
    cost += 3;
  }
}

console.log("To pay: " + cost);
Guess first: what does it print?

To pay: 16. The condition hour <= 2 is true for hours 1 and 2 (5 each), and false for hours 3 and 4 (3 each).

Meaning: At the start of each pass, write the condition with the value of hour at that moment, then its result, for example 2 <= 2: true. This code has two conditions: the loop condition (hour <= 4) and the if condition (hour <= 2). The result of the if condition shows which part runs: the if part or the else part.

⚡ One pass off

Guess first: in the code above, the loop condition becomes hour < 4. How many hours are counted?

Three hours. At the fourth check, 4 < 4 is false, so the loop stops before the fourth hour. It prints To pay: 13.

Meaning: A loop that runs one pass too few or one pass too many has a bug (a mistake in the code) called off-by-one. The cause is almost always < and <= swapped, or a starting number of 0 and 1 swapped. Trace the last check first.

⚡ console.log as a trace

Meaning: For a long loop, add a temporary console.log(hour, cost); at the end of the code inside the loop. Each pass prints one line of output with hour and cost, like one row of the trace table. Delete that line again once you have finished tracing.

What you will learn