i was added to the global scope, rather than the local (function) scope. However, in order for the function to work properly,
i must be scoped locally so that the function can iterate over each child node, and must hold a separate value in each function. As written,
i is reassigned to
0 every time the function is called for a node with children, and since each instance of
i refers to the same global value, this resets the for loops globally - at every level of the recursion.
In practical terms, that means that the function will recursively traverse the DOM until it encounters a childless node, at which point it will become trapped, moving up and down between the childless node and its parent because
i isn't incrementing, preventing the for loop from iterating over the next child or permanently exiting and move up a level in the recursion.