Basic JavaScript - Replace Loops using Recursion - SFWkwIr0Yde1b3HJK-ZZi

I’m having trouble understanding how this statement is true: However, notice that multiply(arr, n) == multiply(arr, n - 1) * arr[n - 1]. Appreciate any help

  **Your code so far**
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
  **Your browser information:**

User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:102.0) Gecko/20100101 Firefox/102.0

Challenge: Basic JavaScript - Replace Loops using Recursion

Link to the challenge:

Welcome to the forum!

Try to play with this code in Python Tutor.
Just for the start.

Maybe in addition try to do some research:
what is base case, what is recursive step, how can we implement them.

Ask more quetions if needed.

edit. Maybe try to implement some classic recursion examples, like factorial(number)

Thank you, I have tried to figure it out with the video lessons on recursion, base case, etc. I guess mathematically I just don’t understand how they’re the same

What do you mean by they? Loops and recursion or…?

multiply(arr, n) == multiply(arr, n - 1) * arr[n - 1] Specifically, this, frankly I just don’t understand how they are equal.

Lets crack it with specific example.
I’l throw here specific input

arr = [2, 3, 5]

n = 2

Apply it to this line.

By the way, be aware that I am not bad when it comes to simple recursion like here.
With more complex examples I am sometimes struggling a lot.

So first step is to multiply the array x 2?

What do you mean by this?

Think I need to rewind quite a bit tbh, thanks for answering though!

I think @admit8490 wanted you to pass those values into the equation you are struggling with, right? So let’s do that:

multiply([2, 3, 5], 2) = multiply([2, 3, 5], 1) * 3;

Do you agree that what I wrote above is using the following equation?

multiply(arr, n) = multiply(arr, n - 1) * arr[n - 1]

If so, then tell me what value each side of the equals sign returns.


Where is the * 3 coming from?

Lets refresh array indexes

let ourArray = [2, 3, 5];

what will be in the console?

1 Like

This is a good question. Remember that the array stays the same the entire time. So the value at the 0th index in the array is always 2, the value at the 1st index in the array is always 3 and the value at the 2nd index in the array is always 5. Now look at the end of the right formula:

* arr[n-1]

So we are multiplying by arr[n-1].

What is the value of n in the left forumla?

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

Once you know the value of n in the left formula then you know what arr[n-1] equals in the right formula.

The console will be 2, 3, 5

So you understand indexes.
Still have questions why




are the same thing for our case?

Yes, I see how what you wrote is the following equation, although Im not getting the same value on both sides of equal sign

What value are you getting for each side? Can you explain how you got those values? Providing us as much information as possible will help us help you.

No, I understand this, n-1 is the last index - 1, or 3 in the array.

wait, not sure about this part

in our case n=2
n - 1 = 1
so arr[n - 1] ===arr[1] which is 3

That would be an explanataion

1 Like

Wait, maybe I got it, on left side of equation I’m getting 6 (by multiplying the first two elements). On the right side I’m getting 6 by multiplying the first element(2) by 3.