FSM JavaScript implementation