Trace the Variables
The trace table
To trace a program, keep a table with a column for each variable. Read one line, work out its right side with the values the table holds at that moment, and write the result in that variable's column. The old value is gone, and a later line can't reach it.| Line | g | m |
|---|---|---|
let g = 9; | 9 | |
let m = 2; | 9 | 2 |
g = g * m % 5; | 18 % 5 is 3 | 2 |
m -= g ** 2; | 3 | 2 - 9 is -7 |
g += Math.floor(m / 2); | 3 + (-4) is -1 | -7 |
It prints -1 -7. The fourth line squares the new g, which is 3 by then. On the last line
m / 2 is -3.5, and Math.floor goes down the number line to -4. Math.trunc would have stopped at -3 and left g at 0, so the two functions give different traces once a negative division leaves a fraction.