Basic JavaScript - Replace Loops using Recursion

Tell us what’s happening:
So I understand the basic function of what’s happening in the question/action needed but what I don’t understand is the first portion of sum(arr, n). The arr when it comes to the mathematical function of it if that makes sense.

How do you come up with the answer when you are not seeming to do anything with the array?

I’ve seen it written out to show how it’s supposed to function but that doesn’t explain to me how it works other than showing me an array with a plus whatever number at the end and sometimes with parentheses.

ex: sum([2, 3, 4, 5] + 2) + 1

I don’t get that at all.

This is what I got based on the examples provided in the lesson:
code so far

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

My question is this:
What is happening on the else portion of the function?
Some people were saying that the + arr[n-1] portion signified how many index locations on the first part to add together sum(arr, n-1).

I hope this makes sense as to what I found confusing.

Your browser information:

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

Challenge: Basic JavaScript - Replace Loops using Recursion

Link to the challenge:

you just need to take a piece of paper and pen and write out the execution of the statements.
You can start out like this:

sum([5,4,3,2], 4), where arr is [5,4,3,2] and n is 4
  - is 4 <= 0 ? no
  - call sum([5,4,3,2], 3) and wait for a result
  ------> n is 3 
        - is 3 <= 0? no
        - call sum([5,4,3,2), 2) and wait for a result
        -------> n is 2
               - is 2 <= 0? no
etc.
etc until n is 0
                         ----> is 0 <= 0? yes, return 0
                 ----> n is 1 here, add arr[1-1] to 0, return 5+0
        ------> n is 2 here, add arr[2-1] to 5, return 4 + 5
  ------> n is 3 here, add arr[3-1] to 9, return 3+9
n is 4 here, add arr[4-1] to 12, return 2+12

all done.
1 Like

@hbar1st

Thank you for taking the time to write this out but I still don’t understand what the array itself has to do with the equation. It looks to me that we can completely get rid of the array and focus on n-1 but I know that is not the case. I know that arr[n-1] needs it but is arr[n-1] using the zero index of lets say 5 (which would be 5-1) in the first part of that array and subtracting 1? That much I get if I understand it correctly.

Is it basically just taking the array as a reference? Kind of like a drawing that you’re doing but the array here is the reference image for the drawing? And then the math of whatever n is - 1 is just telling it what number on the referenced array in zero-index array to add to? Is that correct?

If that’s the case, I definitely was thinking about it too in depth thinking it was some bigger math problem happening behind the scenes.

i’m sorry your question is too convoluted for me to understand.

Why not focus on the code.
Read the code.
Ask me a question about something there?

or if you think there is a different solution , then write it and show it to me and let me know what question you have about it

I was trying to give myself a visual equivalent to understand it.

So what I was trying to say is that the array is sort of like a reference image you would use when you are trying to draw something.

array = reference image
n-1 = drawing was what I was trying to equate that to in my mind.

So basically whatever you get from n-1 (in my previous example n = 5 so 5 -1 = 4) that would be the zero-index location of whatever was in the array (in your example, that would be an array of [5, 4, 3, 2, 1])

In this case (using your array and my equation of 5-1) it would be 1 because the index of 4 is the last item in the array. So the equation would then be sum(1 + 4) correct? And since 5 is not less than 0, the loop would continue until it reached 0 right?

I don’t know what you mean still by reference image.

arr is a series of numbers…
n is a number that tells sum how many numbers to add

this function has no loops

this function calls sum(arr, n-1) unless n is 0 or smaller

this function will wait for sum(arr, n-1) to finish before adding arr[n-1] to the return value

no. that’s not what the code says at all.
sum takes 2 values
the original array and n the number of numbers we want to add together

A recursive function calls itself. So the function sum calls the function sum in the else statement. Since the function sum takes two arguments, an array and a number, you must always pass it those two arguments, even when it is calling itself inside the else statement. So the reason you need the array is because it is required by the function. The function literally won’t work if you don’t pass it an array. And you want to keep passing it the same array because you want to add the numbers in that particular array.

I think this challenge is sometimes hard to grasp because you would not normally use a recursive solution to solve this problem, so it can seem a little forced and thus unnatural. I agree with what @hbar1st first suggested, write it out so you can see how it works. Perhaps use a very simple example so you don’t have so many recursions to handle. For example:

sum([11, 12], 2)

The first time through, n is 2, so it will hit the else statement and execute the following:

return sum([11,12], 1) + 12

But now it must execute sum([11,12], 1) to get the return value for that function call before it can add the return value to 12.

sum([11,12], 1)  // Remember, we are adding this to 12

This is the first recursion. This time n is 1 so we still hit the else statement and execute the following:

return sum([11,12], 0) + 11 // Again, remember, we are still adding all of this to 12

But again, we have to execute sum([11,12], 0) before we can add it to 11 (this is the second recursion). We know that since n is now 0 we will hit the base case and return 0, so the above turns into:

return 0 + 11 // We are still adding all of this to 12

So now we have no more recursions and an actual number that we can then add to the original 12 and we get 23.

1 Like

There is no ‘reference image’.

I want to know the sum of the first 5 entries of an array. That is really easy to compute if I know the sum of the first 4 entries, right?

(sum of first 5) = (sum of first 4) + (fifth entry)

Or

sum(arr, 5) = sum(arr, 4) + arr[4]

Or for the array [6,8,3,1,9]

sum(arr, 5) = sum(arr, 4) + 9

Or

sum(arr, 5) = 18 + 9

But we can only know what sum(arr, 4) is by recursively calling the function.

1 Like

Right I’m not trying to say that it does it all at one time. It’s after it concludes that
n is not less than or equal to 0.

Is n in this case the number of items in the array? If that is the case, then it makes sense that it starts off with 4 in your first example.

If that is the case, I’m trying to figure out what the 4 is doing to the array?
sum(arr, n-1) + arr[n - 1]
sum([5, 4, 3, 2, 1], 4-1) + arr[4 - 1]
so that would be
sum([5, 4, 3, 2, 1], 3) + arr[3]
so would that be the index of the 4th number in the array (which is 1) right?
sum(2, 3) + arr[3]
I know this next part is a jump to say that first part is 2+3 but bare with me:
sum(5+3)
so in total that would be 8 right? And since 8 is greater than 0 it would do the next amount in the array which is 3.

Or is that not how that works?

edit: idk how many times I’ve edited this. Still trying to understand.

No. You can’t drop the arr part of arr[n-1]

Ignoring arr in arr[n-1] breaks the whole thing.

Oh I didn’t realize I dropped that. My bad. Thanks for the callout!

It visually looks small, but its critical! arr[n-1] is a number out of the array.