请问实现图中这样的逻辑用递归可以吗?

请问如果实现这个逻辑可以用递归吗?

clipboard.png

不能简单的用

if
else
因为有很多这样的场景对话 ,我希望用一个方法解决qin

阅读 1.7k
2 个回答

先定义动作序列,然后用一个函数来执行动作序列

var actions = [{
    type: 1,
    msg: '你好'
  },
  {
    type: 2,
    msg: '我是老师Tom'
  },
  {
    type: 3,
    msg: '你是谁'
  },
  {
    type: 4,
    msg: '获取数据'
  },
  {
    type: 5,
    msg: '欢迎你',
    value: true,
    conditions: [{
      type: 3,
      msg: '你上几年级了'
    }, {
      type: 6,
      msg: '执行动作'
    }]
  }
]

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)

没有往回指的箭头,基本上不需要递归

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题