posted 17 years ago
i think a pair of fresh eyes would help me here.
Does the following result in an infinite loop (processNode)
It does..how do i fix this?
<html>
<script>
//Construct a tree
function TreeNode(strText) {
this.childNodes = new Array();
this.text = strText
return this;
}
//add a child
TreeNode.prototype.addChild = function (strText) {
var objNode = new TreeNode(strText);
this.childNodes[this.childNodes.length] = objNode;
return objNode;
}
var localTree = new TreeNode("root");
var child1 = localTree.addChild("l-0");
var child2 = localTree.addChild("l-1");
var child3 = localTree.addChild("l-2");
child2.addChild("l-1.1");
child2.addChild("l-1.2");
child3.addChild("l-2.1");
child3.addChild("l-2.2");
child3.addChild("l-2.3");
processNode(localTree);
function processNode(node) {
alert(node.text);
for(i=0; i < node.childNodes.length; i++){
processNode(node.childNodes[i]);
}
}
</script>
</html>