請問如果實(shí)現(xiàn)這個邏輯可以用遞歸嗎?不能簡單的用ifelse因?yàn)橛泻芏噙@樣的場景對話 ,我希望用一個方法解決qin
2 回答

犯罪嫌疑人X
TA貢獻(xiàn)2080條經(jīng)驗(yàn) 獲得超4個贊
先定義動作序列,然后用一個函數(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)
- 2 回答
- 0 關(guān)注
- 339 瀏覽
添加回答
舉報
0/150
提交
取消