鍍金池/ 問答/PHP  HTML/ 請問實現(xiàn)圖中這樣的邏輯用遞歸可以嗎?

請問實現(xiàn)圖中這樣的邏輯用遞歸可以嗎?

請問如果實現(xiàn)這個邏輯可以用遞歸嗎?

clipboard.png

不能簡單的用

if
else
因為有很多這樣的場景對話 ,我希望用一個方法解決qin

回答
編輯回答
心癌

先定義動作序列,然后用一個函數(shù)來執(zhí)行動作序列

var actions = [{
    type: 1,
    msg: '你好'
  },
  {
    type: 2,
    msg: '我是老師Tom'
  },
  {
    type: 3,
    msg: '你是誰'
  },
  {
    type: 4,
    msg: '獲取數(shù)據(jù)'
  },
  {
    type: 5,
    msg: '歡迎你',
    value: true,
    conditions: [{
      type: 3,
      msg: '你上幾年級了'
    }, {
      type: 6,
      msg: '執(zhí)行動作'
    }]
  }
]

function doAction(action) {
  if (!action) {
    return
  }
  console.log(action.msg)
  if (action.type === 1) {

  } else if (action.type === 5) {
    if (action.value) {
      doAction(action.conditions[0])
    } else {
      doAction(action.conditions[1])
    }
  }
}

actions.forEach(doAction)
2018年5月11日 16:52
編輯回答
詆毀你

沒有往回指的箭頭,基本上不需要遞歸

2018年5月5日 04:36