Coding a Finite State Machine (FSM) in pure JavaScript isn't too difficult. Here are a couple of examples.