# Basic JavaScript - Replace Loops using Recursion

I was able to complete using the example but I do not understand. How does the sum function work? How does the sum [1],0 =0? Or any of the others.

• sum([1], 0) should equal 0.

• sum([2, 3, 4], 1)` should equal 2.

• sum([2, 3, 4, 5], 3)` should equal 9.

Your code so far

``````function sum(arr, n) {
// Only change code below this line
if (n<=0) {
return n;
} else {
return sum(arr, n-1)+arr[n-1];
}
console.log(arr())

// Only change code above this line
}

console.log(sum([4],3))
``````

Your browser information:

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

Challenge: Basic JavaScript - Replace Loops using Recursion

Link to the challenge:

Recursion is a tricky topic at least until the light can be seen at the end of the tunnel.

I found it useful to think of this pattern as a loop with only an if-else statement where the last element/task is determined from the outset and the else clause sets up the subsequent calls to the function until the conditions have been exhausted. Those function calls are all stored in memory until the last call and then the sum is finally calculated like a collapsing set of dominos. There is a very good freeCodeCamp article explaining the mechanics of the process. How Recursion Works — Explained with Flowcharts and a Video

I hope this helps you understand it better.

That’s actually a very interesting question. Let’s try plugging those numbers in. It’s sort of like a piecewise function in math if it helps.

The first example:

Summary

that one has an value of 0 for the variable of `n`. In the first if statement, we check to see if `n` is less than or equal to zero. If it is, we return the value of n.

The second example:

Summary

This one has a value of 1 for the variable of `n` and [2,3,4] for the `arr` variable. As n is greater than 0, we return the value of the sum of everything else we have and the last value in the array.

`````` return sum( [2,3,4], 1-1)+arr[1-1];
``````

Or simplified

`````` return sum( [2,3,4], 0)+arr[0];
``````

`sum( [2,3,4], 0)` returns 0 as established earlier and the value at index `0` is 2.

The third example:

Summary

This one has a value of 3 for the variable of `n` and [2,3,4,5] for the `arr` variable.As n is greater than 0, we return the value of the sum of everything else we have and the last value in the array like last time.

Here’s what we get if we plug the numbers in.

`````` return sum( [2,3,4,5], 3-1)+arr[3-1];
``````
`````` return sum( [2,3,4,5],2)+arr[2];
``````

`sum( [2,3,4,5],2)` calls the function again which will return ` sum( [2,3,4,5],2 - 1) + arr[1 - 1]` which will then return `sum( [2,3,4,5],1 - 1) + arr[0]`. Thus adding it all up.

Here it is written out.
With arr = [2,3,4,5]
`sum(arr, 3)` = `sum(arr, 2) ` + `arr[2]`

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

`sum(arr,1)` = ` sum(arr,0)` + `arr[0]`

`sum(arr,0)` = 0

With a bit of substitution that leads to:

`sum(arr, 3)` = `arr[2]` + `arr[1]` + arr[0]`

or

`sum[2, 3, 4, 5], 3)` = 4 + 3 + 2

And solving this gives us:

`sum[2, 3, 4, 5], 3)` = 9

Happy learning.

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