Finite State Machine implementation on Javascript