From book Introduction of the theory of computation Page 31
// define the states
const states = ['closed', 'open']
// let initial controller is at first state.
let controller
// define configurations
const closed = (input) => {
switch (input) {
case 'neither':
case 'rear':
case 'both':
controller = 0
break
case 'front':
controller = 1
break
}
}
const open = (input) => {
switch (input) {
case 'front':
case 'rear':
case 'both':
controller = 1
break
case 'neither':
controller = 0
break
}
}
// next
const next = (input) => {
switch (controller) {
case 0:
closed(input)
break
case 1:
open(input)
break
}
}
// start business.
controller = 0
next('front')
next('rear')
next('neither')
states[controller] // output the state door is.