Understand recursion

Tell us what’s happening:
Describe your issue in detail here.

I did this challenge and watched a video to better understand recursion. From my understanding recursion is calling the function itself inside it’s function. I did the breakdown on how this work if I call the function sum into my own words. However, I still don’t get how it adds the number 4, 3, 2 which are the values of the index 2,1,0 because if I convert the code

return sum(arr, n -1) + arr[n-1];

it will be like this

sum[2,3,4,5], 2 + 4;
sum[2,3,4,5], 1 + 3;
sum[2,3,4,5], 0 + 2;


but how does it add up?


sum[2,3,4,5], 2 = 4;
sum[2,3,4,5], 1 = 3;
sum[2,3,4,5], 0 = 2;

Like, how does this recursive function works inside?

  **Your code so far**

function sum(arr, n) {
// Only change code below this line
if (n <= 0) {
return 0;
} else {
return sum(arr, n -1) + arr[n-1];
// Only change code above this line

console.log(sum([2, 3, 4, 5], 3))

  **Your browser information:**

User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/98.0.4758.102 Safari/537.36

Challenge: Replace Loops using Recursion

Link to the challenge:

No offense, but this question has been asked so many times now that you are much better off searching this forum for ‘recursion’ than waiting for someone to take the time to type up something again. Here are a few links to get you started:

Trying to understand why recursive code works

Recursion n-1 checking understanding

Explain this recursion function to me!

I want to understand recursion


No offense, but I’ve already read most of it. I can’t still understand completely the exercise that’s why I asked in the forum. I have a learning disability and a complete beginner learning to code this is why I ask if someone could help me.

I think bbsmooth’s point is that this question gets asked a lot. And it gets answered a lot. He linked 4 explanations (two of which no one has clicked). I would recommend rereading all of those and then find 10 more. There is really not much more that we can add than hasn’t been said 20 times already in various posts. If you have a specific question that hasn’t been answered yet, please ask.

We’re not trying to be dismissive. But I don’t see the point of cutting and pasting a previous explanation.

Coding is hard. Recursion is extra hard, especially for a beginner. It takes time. Don’t feel like struggling with recursion means you aren’t cut out for this - I know I struggled with it. One thing that made a big difference for me was understanding what call stack is doing. Conceptually, solving a (physical) pagoda puzzle and imagining how I could make it an algorithm helped too.


Here is an explanation I once gave. If you search, then you can probably find a few other times I’ve explained this one and a couple dozen times I’ve tried to explain recursion in general.

But again, recursion is very weird, it may take a while for it to sink in.

1 Like

One thing I noticed is that your code above is only the solution. You haven’t modified that answer to see what it does when it runs.

function sum(arr, n) {
  console.log("calling sum(arr, n) with n =", n);
  // Only change code below this line
  if (n <= 0) {
    console.log("  base case, returning", 0);
    return 0;
  } else {
    console.log("  recursive case, calling sum(arr, n-1), with n-1 =", (n-1));
    const recursiveCall = sum(arr, n-1);
    console.log("  received value sum(arr, n-1) =", recursiveCall,"with n-1 =", (n-1)));
    const nthValue = arr[n-1];
    console.log("  adding arr[n-1] =", nthValue, "where n-1 =", (n-1));
    console.log("  returning", recursiveCall + nthValue);
    return recursiveCall + nthValue;
  // Only change code above this line

sum([2, 5, 8, 1], 2); // try changing this line

I’m pretty sure I’ve written this out for people before but it looks like you haven’t tried this yet. It might help you understand. It can be hard to understand complex code sometimes when you don’t interact with the code and take it apart.


This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.