What is recursion?

Explained to the 5 year old, in JavaScript.

Develoger
Develoger

--

Stairs do not exist, it is just one step that repeats itself until you reach next floor.

Before we start I need to point out that there is the parent and then there is a parentNode;

// My kids often ask me the regular kid stuff like
if (child.state(‘good’) === true) {
parent.giveCandy();
}
// Or even more often demand that
parent.carry(child);

While() I sure enjoy both I would not allow setting such conditions.
Kids must not connect goodness with rewards. Because being good should be constant.

If (child.status() === ‘sleep’ || child.status() === ‘hurt’) { 
parent.carry(child);
} // now that will Always return true;

Otherwise they need to do it by themselves.
Sometimes that sure can be a problem.
Because you know, they are kids…
Therefor I made up a game, since nothing is hard if you play() (at least if you are not playing Duke Nukem on god damn “Damn I’m Good” difficulty)

I present you the most epic game ever! It is fun, it is easy and boy it is useful. The best part is that it is not restricted to implementation which I have chosen.

Stairs do not exist

Goal of the game is to reach next level, oh I meant floor.
Sounds easy?
Think twice because there is a catch… Instead of using stairs you must use just one step.
No crazy workarounds allowed! While just one condition can be accepted (this is not quite a rule, more of a styleguide).
I almost forgot the most important part, game starts on the first step which is weirdly last step also.

Solution is quite easy…

Read the comments between the lines (of code)

To achieve great goals you need to go one step at a time.

It goes the same with recursion :)
If we are on the first step and all you have is that step, how would you advance all the way up?
One of the ways to do it would be to use the same step over and over again, each time moving one step closer to the next floor!
At the end all you need to do is to stop stepping.

When the Escalator is broken RECURSION occurs!

Liked this article? Please tap or click “︎❤” to help others to reach it… Or consider following me here or on twitter.

--

--