Hello and welcome to our community! Is this your first visit?
Enjoy an ad free experience by logging in. Not a member yet? Register.

# Thread: how to count nested levels in a list?

1. ## how to count nested levels in a list?

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?

• 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...

• wickde; thanks

•

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•