Tracing Assignment and Type
This lesson traces one short JavaScript program line by line, finds its errors, and repairs them. The program keeps a weekend reading log. It's meant to print the pages read on Saturday and on Sunday and their average. Then it asks for the pages read earlier in the week and prints the week's total.
let saturday = 14; // typed in the wrong order:
let sunday = 32; // Saturday was 32 pages, Sunday 14
console.log("Saturday:", saturday, "Sunday:", sunday);
let average = (saturday + sunday) / 2;
console.log("Weekend average:", averge);
let before = prompt("Pages earlier this week: ");
let week = before + saturday + sunday;
console.log("Pages this week:", week);
Output
Press Run to see what this program prints.
Note: This IDE is for experimenting with the code in this lesson. Your changes are not saved, and refreshing the page puts back the original lesson code.
Run the program above to see it print Saturday: 14 Sunday: 32 and then stop with an error. As the lesson finds each mistake, repair it in the program and run it again.
Counts in the wrong order
The program starts by printing Saturday: 14 Sunday: 32, though Saturday was 32 pages. JavaScript gives no message about the first three lines, since each one is legal code.
The repair swaps the two values before the line that prints them. Assigning the value of
saturday to sunday first would replace the 32 in sunday before anything else held it. So the repair first gives that 32 to a third variable, saved. Retyping the two numbers would also repair this program. The swap is what a program needs when two values arrive in the wrong order, and it shows how assignment works.let saved = sunday; // saved holds 32
sunday = saturday; // sunday holds 14
saturday = saved; // saturday holds 32
Tracing the swap
Hand tracing runs a program on paper, one line at a time, recording the value of every variable after each line. The table traces the two count lines and the three swap lines.
| Line | saturday | sunday | saved |
|---|---|---|---|
let saturday = 14; | 14 | not declared yet | not declared yet |
let sunday = 32; | 14 | 32 | not declared yet |
let saved = sunday; | 14 | 32 | 32 |
sunday = saturday; | 14 | 14 | 32 |
saturday = saved; | 32 | 14 | 32 |
When the second swap line changes
sunday to 14, saved still holds 32, since an assignment changes only the variable on its left. The third swap line then gives that 32 to saturday.JavaScript can also swap in one line, with array destructuring,
[a, b] = [b, a]. Its right side is built completely, from the current values, before either name on the left changes, so neither value is lost.A misspelled name
With the counts swapped, the program prints Saturday: 32 Sunday: 14, then stops at the line that prints the average with this error.
ReferenceError: averge is not defined
That line reads
averge, but the variable declared on the line above is average. A name that wasn't declared has no value to read, so the program stops there and the prompt never appears.Correcting the spelling repairs the line, which then prints Weekend average: 23.
One number type
The sum is 46, and
46 / 2 prints as 23, with no decimal point. Had Sunday been 15 pages, the sum would be 47 and the average 23.5, a value of the same number type. Putting Math.floor around the division rounds down, and it would give 23 for both pairs of counts, so the line keeps the plain /.Input is a string
Once the name is repaired, the program reaches the prompt. With 120 typed there, it runs to the end and prints this last line.
Pages this week: 1203214
The total should be 166. Since
prompt() returns the typed text as a string, before holds "120", and the + on the total line joins it with 32 and then with 14. Nothing stops the program and nothing reports the joined total, so this is a second logic error.The repair wraps the prompt in
Number(), so the text is converted as it's read. Now before holds the number 120, and the total line adds 120, 32, and 14 to get 166. A typed string also changes comparisons: a value read with prompt() is never === to a number, since === never treats a string and a number as equal.The repaired program
With all three repairs in place, the program reads as follows. The counts are still typed in the wrong order, and the traced swap lines put them right.
let saturday = 14; // typed in the wrong order:
let sunday = 32; // Saturday was 32 pages, Sunday 14
let saved = sunday;
sunday = saturday;
saturday = saved;
console.log("Saturday:", saturday, "Sunday:", sunday);
let average = (saturday + sunday) / 2;
console.log("Weekend average:", average);
let before = Number(prompt("Pages earlier this week: "));
let week = before + saturday + sunday;
console.log("Pages this week:", week);Output
Saturday: 32 Sunday: 14 Weekend average: 23 Pages this week: 166
That output is with 120 typed at the prompt.