Replace Loops using Recursion - I can't really translate the meaning of the array

Tell us what’s happening:
Describe your issue in detail here.

this part when it says:
else {
return sum(arr, n - 1) + arr[n - 1];
}
and the sum is sum([2, 3, 4], 1)
My question is as following:

If n = 1: sum(arr, n - 1) give what element?
and what element does arr[n - 1] give?

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
}

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

  **Your browser information:**

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

Challenge: Basic JavaScript - Replace Loops using Recursion

Link to the challenge:

sum(arr, n - 1) is not an element, it is the sum of the first n - 1 elements of arr.

arr[n - 1] is the nth element of the array.

sum(arr, n - 1) is not an element, it is the sum of the first n - 1 elements of arr .

Would you please show me how the sum here would look like if n = 1?
Currently, I’m a bit confused as to how the sum is applied on this array.
Since n = 1, it would be sum([2, 3, 4], 1) , 0).
n = 1 for arr[n - 1] is arr[0] here would be [2, 3, 4]. Correct?

The sum of the first 1 items in the array would be the first item - so 2

So “sum” here is just the name of the function.
(arr, n-1), where n = 1 here is literally the first item. That is 2.

arr[n - 1] where n = 1 here is arr[0], that’s [2, 3, 4] correct?

No, arr[0] is 2

sum([2, 3, 4], 2)

would translate to:
return sum(arr, n - 1) + arr[n - 1]
sum(arr, 1) + 3
sum(arr, 0) + 2
0
= 0 + 2 + 3
= 5

right

Ohh ok I get it. I read it wrong.
so if I set the condition where sum([2, 3, 4], 1),
the arr here is [2, 3, 4] and n is equal to 1 in the curve bracket.
Yes?

yes

Sorry, where does +3, +2, and 0 come from?

This part of the return, return sum(arr, n - 1), is where the recursion happens, the loop.
The second part of the return, + arr[n - 1] is the one selecting the actual value of the array, i.e., the 3 and 2.
The 0 is returned when n=0, i.e., when the condition set to stop the recursion is met, as you can check from the if(n <= 0).

Cam anyone explain why sum(arr, n - 1) of sum([2, 3, 4], 1) is 0? T_T

is it because sum(arr, 1 - 1) we get sum(arr, 0), this zero is therefore n <= 0 or something?

What is the sum of the first 0 elements of the array? What do you get when you add 0 items together?

oh, so it’s literally the element position of that array no index here?
Since it asks for 0th element literally, it’s 0. So the sum is 0. xD

No.

sum(arr, n) says “sum the first n elements of arr

arr[n - 1] is the nth element of arr

Two different things

so “sum” here’s not just a function name, it’s literally sum operation?

No, it is a function name. When you call the function, it returns a sum.

ok, if I set sum to be
sum([0, 3, 4], 3)

sum(arr, n-1) means sum([0, 3, 4], 3-1=2) that gives the value of 3 yes?

arr[n - 1] here would be arr[2] which specify the value of 4 yes?

this would give 3 + 4 + 0 = 7?

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

If n = 3, return sum(arr, 2) + arr[2]
If n = 2, return sum(arr, 1) + arr[1]
If n = 1, return sum(arr, 0) + arr[0]
If n = 0, return 0

Now, the recursive function begins to return the respective values:

0 + arr[0] + arr[1] + arr[2]
= 0 + 0 + 3 + 4
= 7

1 Like