Clarity on recursion in Replace Loops using Recursion

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

I solved this but I’m still stuck on the logic.

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], 2));
  **Your code so far**

function sum(arr, n) {
// Only change code below this line
console.log(arr, n-1)
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], 2));


I understand generally how recursion works. But it’s not connecting. Can somehow lay out why this process is working as it is. I want to be sure of this because I think I accidentally solved it the first time and I want clarity and why it works, why do the elements behave as they do.

  **Your browser information:**

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/96.0.4664.45 Safari/537.36

Challenge: Replace Loops using Recursion

Link to the challenge:

Recursion is a very confusion for beginners (and more than a few intermediates and even a few advanced people.) I would recommend searching back through the forum - I know that I’ve posted several explanations.

I also think a big part of understanding recursion is understanding the call stack. I would suggest looking up some youtube videos - sometimes a visual representation helps.

Okay. I watched a few videos and did some reading. That clarified it. I think I misunderstood what elements were doing in the function. I have included the problem below with an explanation of the ‘n’ element (which I was confusing with an index). If you wouldn’t mind, please look over my explanation and tell me if it makes sense:

function sum(arr, n) {
  // Only change code below this line
if(n<=0){
  return 0
} else {
  return(sum(arr, n-1) + arr[n-1]);
}
/*The return adds the first two array values together 
and subtracts from the n-variable (a counter of sorts) 
the number of values that have been added together.  
Depending on the amount n has been decremented, the function 
terminates and returns the sum or runs the function again (if n has 
not reached the termination point of 0)*/
}

Not quite. When the function is called the first time, n (presumably) is greater than 0. So, when we get to:

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

it it can’t finish because there is a function call there. So, the data from this function call is saved on the call stack, so it can figure out what sum(arr, n-1) evaluates to. So it makes that function call. It keeps doing that, building up the call stack with unfinished function calls, each depending on the one above it (thinking of a stack as storing from the bottom up). When we get to the point where n is 0 (called the “base case”), FINALLY a function call can complete. That function calls returns 0, which gets inserted into the sum(arr, n-1) from the previous call, and then that function call can complete, so it passes its value to the function below it which completes, which … turtles all the way down. When we finally get to our initial function call, we are done.

So (at least in this case) no function call finishes until the base case is reached and then all the functions complete, tumbling like dominoes.

It is strange way to think. But there are some computer problems that get really ugly if you try to solve them without recursion.

Don’t be frustrated if this takes a while to completely sink in.