Projectionist — Debugging

Debug code step by step at the village cinema: 20 levels of breakpoints, stepping, the call stack and scope inspection. 20 levels, right in the browser.

What you learn in this game

Welcome to the debugger, or Om Sugeng's cinema. Every step of the program is recorded as one frame of film. You can pause the reel at a line (a clip, or breakpoint), then step forward or back while you read each variable's value board. A bug shows up when a real value differs from the one you expect: spotlight that line, then splice in the right one.

The value boards (the Scope) show every variable's value at the current frame. When a value turns undefined or NaN, that's a big clue. Valid array indices run from 0 to length - 1. nums[length] doesn't exist, so it gives undefined, and sum += undefined becomes NaN. Roll until i = length to see it happen.

Watch a variable as you step. If its value sits still when the logic says it should change, you've found the bug. total + nums[i] computes a number then throws it away at once: total is never reassigned. total += nums[i] (the same as total = total + nums[i]) is what stores it.

= is assignment (it stores a value). === is comparison. if (role = "admin") assigns "admin" to role then tests that value, which is always truthy, so the branch always runs. Roll it and watch: even for "guest", the role board turns into "admin". That is the trace's proof.

The pile of reels beside the projector is the call stack: the chain of function calls that are still running. Every recursive call puts a new reel on top. Correct recursion must shrink each step toward the base case (n <= 1). solve(n) calls itself with the same n, so the pile keeps growing until it overflows. Watch its argument never go down.

On the value boards, watch the quotes: "10" is a string, 10 is a number. The + operator on a string joins them: "10" + 5 becomes "105", not 15. Turn text into a number with Number(base) before adding. The trace shows it: the return value is "105" (with quotes), not 15.

What the levels ask

  1. Roll the reel to the return line, read the boards for a, b and the result, then spotlight the wrong line and splice in the right one.
  2. Roll through the loop. Watch the total board before the loop starts adding.
  3. Roll to the loop's last pass. Watch the i and sum boards when the result turns NaN.
  4. Pick the age = 20 screening and roll it. Which branch does it take? Is that right?
  5. How many times does the loop turn before the function returns? Roll it and count.
  6. Watch the total board on every pass of the loop. Does it change?
  7. Pick the role = "guest" screening and roll it. Is the if branch still taken? Watch the role board change.
  8. Watch the number added to sum on each pass. Does the index move?
  9. Watch the nums board. Does it change on each pass?
  10. Roll it and watch the pile of reels (the call stack) grow without end. Is each call's argument the same?
  11. Pick the n = 200 screening and roll it. Why is it still return true? Check each side of the condition.
  12. Roll until the loop stops. What's the last i that goes in?

And more, 20 levels in all.