【发布时间】:2021-02-21 12:03:32
【问题描述】:
如果 while 循环条件为
var data = {
a: "abc",
c: 0
}
function recurse(data, nodes) {
const first = nodes.shift();
console.log("first-"+first.id, first.t);
if (first.t == "action") {
data.c = data.c + 1;
console.log("exec action", data.c);
} else {
switch (first.t) {
case "if_else":
console.log(recurse(data, first.b[0].actions), 300);
break
case "while":
var brNodes = first.b[0].actions
while (data.c < 7) {
console.log(recurse(data, brNodes), 400);
}
break
default:
break
}
}
return (nodes.length > 0)? console.log(recurse(data, nodes), 200): "Completed"
}
var actions = [
{id:1, t:"action", b:null},
{id:2, t:"action", b:null},
{id:3, t:"if_else", b:[{id:31, t:"branch", actions:[{id:311, t:"action", b:null}, {id:312, t:"action", b:null}]},{id:32, t:"branch", actions:[{id:321, t:"action", b:null}]}]},
{id:4, t:"action", b: null},
{id:5, t:"while", b:[{id:500, t:"branch", actions:[{id:511, t:"action", b:null}]}]},
{id:6, t:"action", b:null}
]
console.log(recurse(data, actions), 100)
这是我在 StackBlitz 上的测试https://stackblitz.com/edit/recurse?file=index.js
【问题讨论】:
-
我在您的
actions中没有看到任何data.c字段 -
@ManuelSpigolon -
data和data.c在 OP 代码块的开头定义。 -
预期输出是什么?
标签: node.js loops recursion while-loop