Undertanding Recursion

Hey guys I am having a couple of doubt of how recursion works, this is the example

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

  // Only change code above this line
}

I have no idea of what is happening in the last part any explanation would be helpful

here is the link

If we want to sum the elements of an array, that is the last number plus the sum of all the elements before it. If we want to sum the first four elements, for example, it would be the fourth number plus the sum of the first three.
So what’s the sum of the first three? It’s the third one, plus the sum of the first two.

The return sum(arr, n-1) + arr[n-1] is “The sum of the first n numbers is the nth number plus the sum of all the stuff before it.”

1 Like

cool thanks I finally got a better undertanding