A trivial state machine written in JavaScript