The following code builds a hierarchy of nodes with 3 fixed levels (3 nodes):
function createNode(name) {
return({name: name, children: []});
}
function addChild(node, child) {
node.children.push(child);
return node;
}
var tree = createNode("");
var subChild = createNode("");
addChild(subChild, createNode("A31"));
addChild(tree, subChild);
How do I alter this code so that the number of levels created in the hierarchy is not fixed. How do I make the creation of nodes dynamic, in that the number of nodes are not known and will vary depending on the some data passed. How is done so that the hierarchy size can be built and defined dynamically. Can loops be applied in the above code to achieve this?