But what is: Recursion? Recursion? Recursion? Recursion?

Adam Crockett 🌀 - Jun 25 '20 - - Dev Community

Google recursion and you will find out exactly what it is, but if you have a moment I will try to explain what it is without going round in circles.

// Recursion is the name for an activity that could go on and on.
Enter fullscreen mode Exit fullscreen mode

True recursion never ends, in JavaScript that should mean a function calls itself over and over.

Now the effect is that a synchronous recursive function will block the program bellow this code (or above) from running until the recursion ends and we leave this function behind for garbage collection. Rucursion never ends unless we provide an exit condition otherwise you will get an overflow, imagine your program is a bathtub, it can only contain a finite amount of water. We say that every 1ms we turn the tap on and then off again filling the bathtub with a drop of water, the water represents memory used and the tub well that's the total memory available to your program 😊 (I know it's not quite like that) if we don't say "when the bathtub is full or reaches maybe 75% capacity, stop running the tap" we get an overflow, more memory was used than was available to your program, without such limits perhaps your computer will catch on fire 🔥.

Here is a recursive function:

// Exotic fruit tree from ebay
const pearAppleTree = {
   fruit: 'apple',
   tallerBranch: {
      fruit: 'pear',
      tallerBranch: null
   }
}

const treeClimber = (branch) => {
   if (branch.tallerBranch) { // exit condition
       console.log(branch.fruit);
       treeClimber(branch.tallerBranch);
   }
}

treeClimber(pearAppleTree);

console.shout('not blocked will carry on with program')
Enter fullscreen mode Exit fullscreen mode

I'm excited to tell you that we climbed to the top of the tree and found some fruit all the way up apart from the top so we stopped and climbed down.

On the first branch, we saw an apple 🍎
On the second branch, we saw a pear 🍐
Then somebody shouted, "your all done climbing that tree? Okay.. well I'm just going to carry on with what I'm doing, il call the garbage person to deal with that fruit because all you did was log it to console for some reason, k bye."

Hope that helps you learn some more programming experience, if not I failed, I failed, I fai...

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .