• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Devaka Cooray
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Jeanne Boyarsky
  • Tim Cooke
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Tim Moores
  • Mikalai Zaikin
  • Carey Brown
Bartenders:

traversing a simple tree in js.

 
Ranch Hand
Posts: 1209
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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>
 
Karthik Guru
Ranch Hand
Posts: 1209
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Any help? I cant understand how this can result in infinite recursion.
Is somethign wrong with my class definition? (Node and addChild).
 
She still doesn't approve of my superhero lifestyle. Or this shameless plug:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic