# Recursion statement

``````function walkTree(node) {
if (node == null) //
return;
// do something with node
for (var i = 0; i < node.childNodes.length; i++) {
walkTree(node.childNodes[i]);
``````

i got this on an explanation of recursion. i know recursion calls on itself, but the for statement, i dont understand the “i<node.childNodes.length” - why is it written as it is?

Hello bvernwashington!

node.childNode is an array and the variable “i” is equal to the iteration of the for loop (how many times the loop has run).

node.childNode.length would display the number of items in the array. So long as the variable “i” is less than the number of items in the array, the for loop will continue running, and increment the value of “i” each time it runs.

We want to call the function walkTree on each child of the node that we pass to the function.

So our limit is the number of child nodes and because child nodes are stored as an array, we can say `node.childNodes.length`.

In other words, we’re calling the function walkTree(node) on each child of the node.

Important part is the beginning:

``````if (node === null) {
return;
}
``````

So we are not going to run the function when there is no child.

components of a recursive function:

• take some `data` as an argument
• set an end condition to return `data`
• perform some logic to `data`
• return a function call with new ‘data’ as a parameter

Thanks everyone - hunted down my book, and I’m going to look into it more before finishing this challenge!