View Full Version : how to count nested levels in a list?
brothercake
07-24-2003, 07:55 PM
I'm having a bad brain day ... feels like this should be really obvious, but I can't see it ..
Given an unordered-list with an unknown number of nested lists, how would I find the deepest number of levels it goes to?
Vladdy
07-24-2003, 08:20 PM
function recursion(depth,element)
{ currentDepth = depth + 1;
for(var i=0; i<element.childNodes.length; i++)
{ if(condition)
{ newDepth = recursion(currentDepth,element.childNodes[i]);
currentDepth = newDepth>currentDepth?newDepth:currentDepth;
}
}
return currentDepth;
}
recursionDepth = recursion(0,topelement);
It's late in the afternoon and my brain as tired too, but this should give you the depth of recursion...
brothercake
07-25-2003, 03:11 AM
wickde; thanks :thumbsup:
vBulletin® v3.8.2, Copyright ©2000-2012, Jelsoft Enterprises Ltd.