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.
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')
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...
Top comments (12)
When you google that, you'll find one of Google's witty jokes instead:
π or wait, maybe you actually meant to refer to this and I was completely missing the point...
Anyway, nice insights on the recursion! Thinking in recursion helps me reuse many parts of the code and simplify what could be a complex nested process.
Surprise π₯³π€£
Interviewer - Explain recursion, I will hire you.
Me - Hire me, I will explain recursion to you.
It's more like deadlock
Indeed technically it's recursive as it does just go around in circles.
The interview lasted for 65 years, unfortunately everyone died.
Nice article, great to see some tree traversal. I also published an article about recursion three days ago (marcoslooten.com/blog/how-to-use-r...) and I wasn't sure if I could explain it with a tree so I used other examples, but you definitely made it understandable!
Good on you Marcos, il certainly checkout your blog.
Another great recursion explanation at codeburst.io/learn-and-understand-...
Il take the implication that I did a good job too haha thanks π I tend to write silly impactful 2min reads as I only blog on my phone.
Also check this article for a great explanation: dev.to/adam_cyclones/but-what-is-r...
Ha!!! Very good π¦