Basic JavaScript - Replace Loops using Recursion

Tell us what’s happening:
Describe your issue in detail here.
Hello, Ive been on this for a few days now. This is just the example problem but I want to understand it. Can someone break this down for me ? I ran it through this visualizer and that helped but I dont really see how it got the return value of 6 at step 9. here is the visualizer link
https://pythontutor.com/render.html#code=function%20multiply(arr,%20n)%20{ %20%20%20%20if%20(n%20<%3D%200)%20{ %20%20%20%20%20%20return%201%3B %20%20%20%20}%20else%20{ %20%20%20%20%20%20return%20multiply(arr,%20n%20-%201)%20*%20arr[n%20-%201]%3B %20%20%20%20} %20%20} %20%20console.log(multiply([2,%203,%204],%202))&cumulative=false&curInstr=9&heapPrimitives=nevernest&mode=display&origin=opt-frontend.js&py=js&rawInputLstJSON=[]&textReferences=false
Your code so far

function multiply(arr, n) {
    console.log(arr[n - 1])
    if (n <= 0) {
      return 1;
    } else {
      return multiply(arr, n - 1) * arr[n - 1];
    }
  }
  console.log(multiply([2, 3, 4], 2))
  

Your browser information:

User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/105.0.0.0 Safari/537.36

Challenge: Basic JavaScript - Replace Loops using Recursion

Link to the challenge:

If in doubt, write it out :slight_smile:

I’m going to assume you understand that if the second arg (n) is 0 then the function will always return 1. So if we call the function as:

multiply([2,3,4], 0);

That the return value will be 1.

Now call it with n = 1:

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

Since n is greater than 0 the else block will execute. Replace the variables with their actual values in that return statement and you get:

return multiply([2,3,4], 0) * 2;

Do you see how we got the values for that return statement? This is important, you can’t move on until you understand how the return statement works.

The return statement is doing two things:

  • Calling multiply([2,3,4], 0) which we already know is going to be 1.
  • Multiplying the value returned from multiply([2,3,4], 0) (which again, is 1) by 2.

So that return statement can now be written as:

return 1 * 2;

And thus multiply([2,3,4], 1) returns 2.

Now why don’t you try explaining the return value for multiply([2,3,4], 2)? Write it out like I did above.

1 Like

Okay ive been going over this and I just dont see where the 2 in the return comes from. I think the return is where im hung up. In the multiply( [2, 3, 4,], 2) example I gave everything makes sense until it returns 6.

It’s the arr[n - 1] bit. In that case n - 1 is 0, so it evaluates to the 0-th index of arr, which is 2.

1 Like

OH MY GOD… … Thank you!

thanks so much! this really helped me understand the problem

function multiply(arr, n) {
  if (n <= 0) {
    return 1;
  } else {
    return multiply(arr, n - 1) * arr[n - 1];
  }
}
console.log(multiply([2, 3, 4], 2))

/* n = 2;
if statement n is not <= 0;
else statement broken down n - 1 * [n - 1] or 2 - 1 * [2 - 1] or 1 * 3 = 3;
if statement 3 is not <=0;
else statment n - 1 * [n - 1] or 3 - 1 * [3 - 1] or 2 * 3 = 6;
Does this make sense and look correct? also, if so, why does it stop executing when it returns 6?*/

This is not quite correct. The else statement in the code is:

return multiply(arr, n - 1) * arr[n - 1];

What you have written does not look like the else statement. You need to replace n and arr with their actual values in the actual else statement.

Im confused [n - 1] is 2 - 1 = 1 and 1 is the [1] index position in the array [2, 3, 4] which is 3 correct?

Yes, when the function is called passing 2 in for the second arg then inside the function n - 1 will equal 1 and thus arr[n-1] will be arr[2-1] which will be arr[1] which is the value 3 in this case. So you know that the part of the return statement after the multiplication sign is 3:

return multiply(arr, n - 1) * 3;

Now explain the part on the left.

else statement broken down multiply(arr, n - 1) * arr[n - 1] or multiply([2, 3, 4], 2 - 1) * arr[2 - 1] or multiply([2, 3, 4], 1) * arr[1] = 3;

How did you get that this equals 3? Especially since you know that it really equals 6.

And you really haven’t explained the left side yet. We already established that this breaks down to:

return multiply(arr, n - 1) * 3;

Explain to me exactly what multiply(arr, n - 1) is doing. I would start by replacing the variables with their values.

I got 3 because index position 1 in the array is 3 and n which is 1 * 3 = 3.
I think I am very confused on what multiply(arr, n - 1) is doing now. I thought it was just multiply([2, 3, 4], 1)?

That’s perfect. And what does that give you? And do you understand why?

I dont think I understand the question “what does that give you”. Am I multiplying something within ([2, 3, 4], 1)?

We are at this point:

return multiply([2, 3, 4], 1) * 3

Explain to me in as much detail as possible what this returns and why. There is nothing tricky going on here. If you think it is obvious then great, explain the obvious to me. But convince me that you understand how the recursions is working. Because what you wrote above:

n = 2;
if statement n is not <= 0;
else statement broken down n - 1 * [n - 1] or 2 - 1 * [2 - 1] or 1 * 3 = 3;
if statement 3 is not <=0;
else statment n - 1 * [n - 1] or 3 - 1 * [3 - 1] or 2 * 3 = 6;
Does this make sense and look correct? also, if so, why does it stop executing when it returns 6?

Does not sound like you understand how the recursion is working. Perhaps you didn’t write that as clearly as you wanted to? Maybe you do understand how it is working? I’m just trying to help you confirm that you do. But if you do understand and don’t need any more help then we can end this conversation. If that’s the case, then I would suggest you try explaining the solution to yourself for a different set of numbers, such as

multiply([3,7,11], 2)

Make sure you can completely explain to yourself how you get the answer.

I appreciate all your help so much, I do not think I fully understand this at all lol. Okay I will work through it and get back to you. Thank you agin sooo much because this is very frustrating and its making my brain hurt.

No worries, recursion takes time. Just remember, you have gone this far:

return multiply([2, 3, 4], 1) * 3

Does multiply([2, 3, 4], 1) look familiar? It should. Look at my comments above. I could swear we already solved this.

I honestly feel like im completely lost again now and dont have any idea what im doing wrong. If i am at

return multiply([2, 3, 4], 1) * 3

All I get from that is 1 * 3 = 3
I know im doing something wrong.
im looking at it like this ([2, 3, 4], 1) the 1 is n and then we multiply that by 3

You are leaving out an important part though. You can’t just ignore things that are there :slight_smile: You should be looking at it as:

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

Do you see how you are leaving out the function call? This is calling the function multiply passing it the arguments [2,3,4] and 1. Functions return a value. So what does that function call return? Look above at my earlier comments.