 # Recursion Challenge help

Tell us what’s happening:
How does multiply(arr, n) == multiply(arr, n - 1) * arr[n - 1]?
I’m trying to understand how this line of code works and how it does the same thing as the example loop provided.

``````
function sum(arr, n) {
// Only change code below this line

// Only change code above this line
}

``````

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

Challenge: Replace Loops using Recursion

This line says, in words, that “the product of the first `n` elements in the `arr` is the same as the product of the first `n - 1` elements in `arr` multiplied by the `n`th item in `arr` (which is `arr[n - 1]`)”.

Does that help or can I explain better?

If you could simplify that it would be greatly appreciated. I am able to complete the challenge based on the example code provided, I just don’t fully understand what all the code is doing. If you could include another example and break down how it works that would be awesome.

the multiply(arr, n - 1) is the part I’m having trouble understanding. What does this do the first time the function is being called and how is it not the same as the arr[n-1] value?

So, let’s look at an example.

``````arr = [3, 2, 4]
n = 3
``````

with the function

``````function multiply(arr, n) {
if (n === 0) {
let result = 1;
return result;
} else {
let smallerCase = multiply(arr, n - 1);
let result = smallerCase * arr[n - 1];
return result;
}
}
``````

Now, in the case above, we

1. Call `multiply(arr, 3)`
a) if test: `3 !== 0`
b) else: so we call `multiply(arr, 2)`
2. Calling `multiply(arr, 2)`
a) if test: `2 !== 0`
b) else: so we call `multiply(arr, 1)`
3. Calling `multiply(arr, 1)`
a) if test: `1 !== 0`
b) else: so we call `multiply(arr, 0)`
4. Calling `multiply(arr, 0)`
a) if test: `3 !== 0`, we return `result = 1`

Now we can finish 3)
3)
c) `smallerCase = 1` and `result = 1 * 3`
d) we return `result = 3`

Now we can finish 2)
2)
c) `smallerCase = 3` and `result = 3 * 2`
d) we return `result = 6`

Now we can finish 1)
1)
c) `smallerCase = 6` and ` result = 6 * 4`
d) we return `result = 24`

Whew, that was a lot. The big picture is that in recursion, we make a smaller version of the problem until we get down to the smallest version of the problem.

Thanks so much for your help, for future reference I could not find the 5 minute video on recursion that is stated on the watch a video link. The provided articles somewhat helped but being able to watch that video would of probably helped me tremendously. Thanks again.