My code below runs fine if the while loop condition is < 6 (i.e. one loop), but failed if it is set to < 7 or more (i.e. more than 1 loop)
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)
Here is my testing on StackBlitz https://stackblitz.com/edit/recurse?file=index.js
data.cfield in youractionsdataanddata.care defined at the beginning of the OP's code block.