Basic JavaScript - Replace Loops using Recursion

Tell us what’s happening:
can someone please explain what is happening on the “return sum” line, I got the gist of recursion like how the code has “n - 1” in order to get the preceding number.

What I don’t understand is why is the name of the function called and what does
“+ arr[n - 1]” do in the code?

Your code so far

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

Your browser information:

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

Challenge: Basic JavaScript - Replace Loops using Recursion

Link to the challenge:

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

If i rewrite this line in plain English, I would say it like this:

I want to return the sum of a list of numbers, but all I know how to do is add 1 number to another number. So I will just add the number at the end of the list to whatever the sum of the numbers of the rest of the list are.
I’m not going to worry about what that sum is.
I have a friend called ‘sum’ who knows how to add things so I will just give my friend the array and tell them to add everything except the last number (which I will add later on myself).

What do you think? Does this help a bit?

I am sorry but I still don’t understand the code and how I would relate it to what you just said.

okay maybe a more general description then.

Recursion involves a type of delegation.
I - the sum function - can only add 2 numbers to each other
so how can I add many numbers to each other.

I can add the last number in the array to the number which is equal to the
[sum of the others]

But how do I get the [sum of the others]
Don’t care. Just pass the array to another version of me to worry about that.
sum(arr, n-1) is just that. The original sum is passing the array and the size of the problem is reduced by one as the original sum is handling the last number only.

Another way to say this is:
if the the array was [1,2,3,4] and n is 4
then we see something like below happening under the covers:

sum of [1,2,3,4],4 calls                         returns 4 + [3 + 2 + 1 + 0]
    sum of [1,2,3,4], 3 calls                    returns 3 + [2 + 1 + 0]
        sum of [1,2,3,4], 2 calls                returns 2 + [1 + 0]
            sum of [1,2,3,4], 1 calls            returns 1 + [0]
                sum of [1,2,3,4],  0             returns 0

imagine there is a diagonal arrow pointing down from sum to sum to sum
and imagine there is a straight arrow point up from returns 0 to the previous line to the previous line etc.


Ok so I understand why “return sum(arr, n - 1)” is written but what about “arr[n - 1]”, what does it have to do in the code?

Well, you’re trying to sum members of the passed array, aren’t you? arr[n - 1] is just saying: take the last item in array and sum it with the result of first expression.

1 Like

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