I feel bad for not being able to understand this line. Please help

But how does this line of code work?
If i was a computer, what would I do?

function sum(arr, n) {

  // Only change code below this line

  if (n <= 0){

    return 0;

  }

  else{

    return sum(arr, n - 1) + arr[n - 1]// I understand that this is function.

  }

  // Only change code above this line

  // But I don't understand  how sum([1], 0) equal 0.

}

Don’t feel bad, recursion is a very confusing topic, especially for beginners. I don’t think anyone gets it right away. It took me a long time to really understand what was happening.

1 Like

When in doubt, log it out. Try putting in log statements to understand the flow:

function sum(arr, n) {
  console.log('\nn =', n, '- entering sum, arr =', arr)
  if (n <= 0) {
    console.log('n =', n, '- base case reached, returning 0')
    return 0;
  }
  else {
    console.log('n =', n, 'in else before recursive call')
    const ret = sum(arr, n - 1) + arr[n - 1]
    console.log('n =', n, '- in else after recursive call and returning', ret)
    return ret
  }
}

console.log('\nFinal result:', sum([1, 2, 3, 4, 5], 3))

I think one of the keys to understand is that when JS encounters this:

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

it cannot evaluate it because it has to call that function first. So, you end up with a bunch of incomplete function calls on the call stack until you reach the base case and then they all can complete, one by one, until you get to your original function call that returns the number.

But again, this is confusing stuff. Go easy on yourself.

Thanks Kevin :hearts:

I hope I do understand. Let me console.log my code to see what is going on

I think my issue started from this.
How is this true?

‘multiply(arr, n) == multiply(arr, n - 1) * arr[n - 1]’

Think in terms of algebra instead of JS.

multiply([1, 2, 3, 4, 5]) = multiply([1, 2, 3, 4]) * 5

or

(1 * 2 * 3 * 4 * 5) = (1 * 2 * 3 * 4) * 5

Again, this is algebra, not JS code. The point of recursion is that you use that logic to keep breaking the right hand side down into smaller problems:

(1 * 2 * 3 * 4 * 5) = (1 * 2 * 3 * 4) * 5

becomes

(1 * 2 * 3 * 4 * 5) = ((1 * 2 * 3) * 4) * 5

becomes

(1 * 2 * 3 * 4 * 5) = (((1 * 2) * 3) * 4) * 5

becomes

(1 * 2 * 3 * 4 * 5) = ((((1) * 2) * 3) * 4) * 5

The point is that now it is just a bunch of single elements being multiplied, one at a time.

I say that as if that is “simple” - it’s not, it’s still confusing, but that is what is happening, essentially.

I surprisingly understood this.
To an extent.

Although I am still thinking can n be any number?
Please correct me if I am wrong

Well, I think logically n couldn’t be larger than the size of the list. It wouldn’t make sense to sum the first 5 elements of an array of 3 elements. But baring that, sure. Try it out with different numbers, see what you get.

Hey Kevin, thanks for your support few weeks ago.
I won’t say I am now an expert, but I think I understand how recursion works now.
I even wrote a fibonacci program after racking for days.
I felt it was right to keep you posted.
Thanks again.

1 Like