# Recursiveness and the idea and the formula

Tell us what’s happening:
Describe your issue in detail here.
Please help me understand multiply(arr, n) == multiply(arr, n - 1) * arr[n - 1] as I have no idea whatsoever as to what it means. I greatly appreciate your help. I do not understand this all.

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

// 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/91.0.4472.124 Safari/537.36`

Challenge: Replace Loops using Recursion

Let me call the function here multiply(arr,n) as:

multiply([1,2,3],3)

Here we have taken an array of three digits

[1, 2, 3] here n=3

is n<=0? no so we go to the else statement

here we call the multiply function again but we pass the length of the array as 1 less than it’s previous value, that is we call:

multiply([1,2,3],2)

to what ever value is returned we multiply the last digit of the array that is arr[n-1] in this case: arr[2] as array index start with 0, the last element is arr[2]

return multiply(arr,2)*arr[2]

That is multiple the last element of the array and call the multiply function which is at the very beginning again but this time tell it to multiple first two digits.

so before returning anything out of the function, we call the multiply function again, this time passing in n=2

again we check inside the multiply function,

is n<=0 that is is 2<=0 no! so we skip and go to the else part there

again same as before we call the multiply function again this time passing in n=2-1=1 and then multiplying the result with the second last element or in this case the second element of the array as arr[2-1]=arr[1] which is the second element of the array.

So we call the function again and until we get back the result we cannot return anything back to the previous function call mentioned above which is also waiting to receive the results or whatever is returned. (Make a picture it helps a lot!)

Now we call the multiply function again,

multiply(arr,1)

Again we have 1<=0 as false so we skip and do the same as before. Call the multiple function again with n=0 and then multiply whatever’s returned with the first element of the array arr[0].

Now time to call multiply[arr,0] since n-1=1-1=0

This time 0<=0 is true so we return 1 back to the function call.

So in the previous function call is waiting to receive the results which is 1! It then sends 1 multiplied by the first element to the function call before it which then sends 1 multiplied by the first and second element to the previous function call which finally returns the 1 multiplied by the first second and the third element out of the function to the very first function call!

Here is a list of function calls. Read the => part at the end starting from the bottom one:

At first: multiply(arr,3)=>returns 1*arr[0]*arr[1]*arr[2] out of the function finally!

Then: multiply(arr,2)=>returns 1*arr[0]*arr[1] to the function call above it

Then: multiply(arr,1)=> returns 1*arr[0] to the function call above it

Then: multiply(arr,0)=> returns 1 to the function call above it.

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