This is how you define a function in ECMAScript6

Screen Shot 2017-10-13 at 7.32.40 AM.png

let is the new var
arrow function is the new way.

And this is a simple method that counts down from 10.

Screen Shot 2017-10-13 at 7.37.29 AM.png

Notice this method calls itself. That’s recursion.

Here is another example. Say you have a datastructure has contains a parent, and then several children that reference other children like this.

Screen Shot 2017-10-13 at 7.40.22 AM.png

And say you want to convert this into a form where you could visualize this as nested subdirectories. Say a tree structure like this.

Screen Shot 2017-10-13 at 7.43.18 AM.png

Let’s start by making a function

Screen Shot 2017-10-13 at 7.45.06 AM.png

Then let’s call our makeTree function with categories and a starting parent of null. This will give us the first node with parent === null.

Screen Shot 2017-10-13 at 7.47.54 AM.png

Then what we are going to do is to take each element and assign it a node based on it’s id.

Screen Shot 2017-10-13 at 7.49.39 AM.png.

So now we have a node called animals.

Now here is where the magic happens. We are going to take that node and create the subtree by calling makeTree again passing in the same categories and parent for the next wave of subnodes, which happens to be this nodes id.

Screen Shot 2017-10-13 at 7.54.51 AM.png

So that’s an example of recursive. My other favorite example of recursion is in a binary tree. But we can look at that later.

Check out mpj’s excellent series for my videos like this.