Finite State Automaton (or finite-state machine) in JavaScript