Javascript Dynamic Automata