Tracing shared state through queued events

A single-threaded program registers three handlers that all update the shared variable total. The object clock emits a tick event at regular intervals.


JavaScript
let total = 0;
button.addEventListener("click", () => { total += 4; });
clock.addEventListener("tick", () => { total *= 3; });
field.addEventListener("keydown", () => { total -= 2; });


After setup, these events reach the event queue in this order: tick, click, keydown, mouseover, click, tick. No handler is registered for mouseover. What is the value of total after the queue has emptied? Give your answer as an integer.

Create a free account to get started

Unlock full course notes by signing up

  • Video lessons taught by top educators
  • Practice questions with guided solutions
  • Written review notes for reference
  • Expert answers anytime from course tutors

More Event-Driven Programming Questions: