# Can someone explain me how recursion works?

I am stuck at this part of the curriculum. Link: https://www.freecodecamp.org/learn/javascript-algorithms-and-data-structures/basic-javascript/replace-loops-using-recursion

After I read the hint I still don’t understand recursion. Can someone explain this to me?

I gave up programming for a few weeks because I am stuck here. That is a pity of course. I also struggly with concentration/ motivation a lot. I would be glad to recieve advice on this + I would love to have a study mate “on distance” or an accountability partner if you will. I want to secure my future, I want to work in the IT field, I want to learn how to program so I can get a job as a programmer.

Does this topic help?

For study mates, I have seen a few groups post around here on the forums. Maybe do a search and you can find one for your area.

If you have any questions please respond here

1 Like

I have been trying to figure a way to explain recursion in a way that makes sense for a while, because it is a very confusing subject. We use it all the time, but we don’t really unpack the thought process we use when we do - we simply take it for granted.

With that in mind, I wrote a blog post about recursion. Specifically, it uses recursion to build an nth-power function (and yes, I know `Math.pow()` will do the same thing, but it seemed to me a natural, understandable way to look at recursion).

Give it a read, let me know if it helps: https://portfoliostuff-parenttobias.codeanyapp.com/2020/01/29/recursion-all-the-way-down/

1 Like

Old CS Joke:
A Novice once came to the Master and said, “Master, you are most wise and know of many things. I would ask you a simple question: how many grains of rice are in my bowl?”

The master took one grain out of the bowl, pointed at the bowl and said “One more than that”. And the novice was enlightened.

2 Likes

I like the story! but what has this to do with my question?

Just a joke to lighten the mood is all. It’s a reference to how you count elements in a linked list, by using a recursive algorithm. In code, it would look like this:

``````function size(arr) {
if (!arr.length) return 0 // The Master didn't mention this base case
else return 1 + size(arr.slice(1))
}
``````

Obviously you wouldn’t do this in javascript just to find the length, but it’s the basic shape of any tail-recursive algorithm. I also have a decent explanation of recursion percolating in my head, but actually laying it out in plain terms is difficult. Turns out that teaching is pretty hard.

1 Like

Actually, that’s a very recursive joke. Had the student NOT been enlightened and asked the question again, the teacher would have removed one more and said “one more than that”… until the bowl contained one grain of rice. Then the teacher could add a grain and a grain and a grain… thus answering the student’s question via recursion!

Nicely done, @chuckadams - recursion is everywhere.

1 Like