0

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

3
  • I didn't see any data.c field in your actions Commented Nov 9, 2020 at 15:04
  • @ManuelSpigolon - data and data.c are defined at the beginning of the OP's code block. Commented Nov 9, 2020 at 15:06
  • what is the expected output? Commented Nov 9, 2020 at 15:13

2 Answers 2

1

looks like i have figured it out myself... the array was passed in by reference, and after the first loop it was consumed to an empty array. i replaced it with [...] spread

console.log(recurse(data, [...first.b[0].actions]), 400);

and this should solved

Sign up to request clarification or add additional context in comments.

Comments

0

Your code is doing this:

const first = nodes.shift();

And, then attempting to access:

 first.b[0].actions

And, nodes.shift() removes the first element from the nodes array. Your nodes array has 6 items. If you try to call your function 7 times by setting the condition to while (data.c < 7), you will end up with an empty nodes array and thus an undefined value in first on the last iteration and that will cause an error when your code attempts to access first.b[0].actions on the last iteration.

1 Comment

@kkgan - Did this answer your question? If so, you can indicate that to the community by clicking the checkmark to the left of the answer and that will also earn you some reputation points here for following the proper procedure.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.