 # HOW is `multiply(arr, n) == multiply (arr, n - 1) * arr[n - 1]`?

Tell us what’s happening:
The code is from the note on `Replace Loops using Recursion`. I really don’t understand how `multiply(arr, n) == multiply (arr, n - 1) * arr[n - 1]`

``````  **Your code so far**
``````
``````
function multiply(arr, n) {
if (n <= 0) {
return 1;
} else {
return multiply(arr, n - 1) * arr[n - 1]; //I don't understand what's happening here
}
}
//the following were not part of the note
var  myArray = [2,3,4,5];
var myAns = multiply(myArray,3);
console.log(myAns);
``````
``````  **Your browser information:**
``````

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

Challenge: Replace Loops using Recursion

Have you tried going step-by-step through it, with considering what exactly each variable is at that time?

Hi Jedidiah,

I agree, the recursion is hard to fully grasp without understanding the Mathematical meanings behind it. In the simplest terms, think of the factorial number concept; where you multiply the number with the next following decremented number.

For example 4! is 4 x 3 x 2 x 1 or in a more generic form n! = n x (n-1) x (n-2) … x 1. You will always end with 1 at the end of the loop ( notice the if conditional statement n<=0 that set the base).

We know that 4 x 3 x 2 x 1 is the same as 4 x (4-1)X(3-1) x (2-1) . The number is pointing back at itself at every multiplication operation.

In the recursive function above:
The multiply (arr, n) : n becomes n-1 in the next instance which is the same as decrement loop .

arr[n-1] : returns the number in the array for the next index in the array. Then it’s multiplied with the output.

I hope this helps.

1 Like

I like to think of the core idea of recursions ‘make the problem smaller’.

If I want to multiply the first 5 elements of an array together, the smaller version of the problem is to multiply the first 4 elements of the array together.

I r use the answer to the smaller problem to make the answer to the bigger problem. So, if have the product of the first four elements, then I can multiply that number by the 5th element to find the product of the first 5 elements.

The base case is the ‘smallest’ version of the problem. In this case, the product of zero elements is 1.

var myArr = [1,2,3,4]

function multiply(arr, n) {

``````if (n <= 0) {

return 1;

} else {

return arr[n - 1] * multiply(arr, n - 1); // look at this

}
``````

}

console.log(multiply(myArr, 4))

look at this: notice how i flipped the base arr[n - 1] and multiply(arr, n - 1);, easier to understand.

this explain better what’s happening.

The iteration goes like:
arr[n - 1] * calls it self (multiply(arr, n - 1)) and function returns
arr[n - 1] * call it self and function returns arr[n - 1] … so it multiplies
arr[n - 1] all the time.

At the end it also does total * 1 = then print the result

if you change 1 with 2, you’ll get the result multiplied by 2.

Thanks everyone for your help in understanding this. I understand this better from the mathematical concept of factorial.

However, this `return` statement was a major source of confusion. It seemed the function `multiply()` was being multiplied by `arr[n-1]`. This really got me confused.