Referencing an Array inside a Recursive Function

So I understand how basic recursion works now. But I am having a hard time understanding why the second else statement’s ‘arr[n - 1]’ n is an index for the array.

The way its written, I would expect to get the result of [1, 2] [n-1] instead of arr[1]. From what we learned, dont we access arrays by referencing their relative variable’s name, so we would need to have made the arr a variable? The main point is I dont understand how the code references the array when ‘arr’ is simply a parameter for the array, not a variable

  **Your code so far**

function sum(arr, n) {
console.log(arr, n - 1); console.log(arr[n-1]); if(n <= 0) {
  return 0;
} else {
  return sum(arr, n - 1) + arr[n - 1];

console.log(sum([1, 2], 2));

After first function call (console.log(sum([1, 2], 2))

sum ( [1, 2], 1) + arr [1] ---- why is arr[1] = 2?
    sum ( [1, 2], 0) + arr[0]
    ^ returns 0 but why is arr[0] = 1?  
  **Your browser information:**

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

Challenge: Replace Loops using Recursion

Link to the challenge:

The array is never mutated, so the values of the array indexes don’t change. The only thing that changes is the index: n.

I might be able to think of a way to clear up your thinking here. Recursion is tricky!

A parameter is a variable. Inside of the function sum there is the identifier arr which holds the value of whatever was passed in as the arr argument.

Oh!!! I might have missed a reading or something that said parameters are also variables. Now I understand how it references the array! Thank you! I was thinking of it as not a variable so I thought when the function was called, it would pass the contents inside the arr like it did for ‘sum(arr, n - 1)’

1 Like

I was wondering if you could also explain this code

function countdown(i) {
    console.log(i);  if (i <= 1) {  // base case
    } else {     // recursive case
        countdown(i - 1);

Once ‘i’ reaches to equal 1, it says to return, but it doesnt have anything else after. What does it mean when it simply says return. In the original code I asked about, it said to return 0 but this one is empty

It will just return undefined. You have the return there just to stop the code from executing any further.

I see, thanks, last question because I feel so bad lol, once the stack reaches the top, where the code is

sum ( [1, 2], 1) + arr [1]

‘sum( [ 1, 2], 1)’ would now equal 1, so what array does arr[1] reference to get it to equal 2? Does it reference the initial function call’s array, the one within the console.log?


I think you should execute all of this code by hand, it might become more clear to you.

It’s difficult to answer your question other than to say you’re getting a little lost, but if you slowly evaluate the code yourself, one step at a time, I think you will understand better what is going on.

I’d also recommend just not worrying about recursion right now… There are few cases where you will need recursion as a beginner JS programmer… you can just use a for loop.

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