Basic JavaScript - Replace Loops using Recursion

Everything was going great until “recursion” came into play and i think this was where things got mixed up for me hence why the countDown exercise is frustrating me presently, i have gone thru the breakdown explanation of recursion there but the correlation of how they make recursion possible is still lost on me, so am back here retracing steps . From the example to be observed:

function multiply(arr, n) {
    let product = 1;
    for (let i = 0; i < n; i++) {
      product *= arr[i];
    }
    return product;
  }

What does i < n; mean here? i get why i cant use i < arr.length but how does ‘i’ being less than ‘n’ multiplies the first value of the array to make this work:

function multiply(arr, n) {
    let product = 1;
    for (let i = 0; i < n; i++) {
      product *= arr[i];
    }
    return product;
  }

multiply([10, 2, 3], 2);

From here i dont get how that lead to ‘n’ being the same as ‘n-1’, same reason why my solution worked either, i just followed the steps not why or how. Please help, i`m so confused about recursion.

Your browser information:

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

Challenge: Basic JavaScript - Replace Loops using Recursion

Link to the challenge:

Let’s look at that part of the code:

for (let i = 0; i < n; i++) {

So i starts at 0 and the loop keeps looping until it is equal to n. The variable n is passed into the function as the second argument and represents the first n items of the array you want to multiply together. So if you want to multiply the first three numbers in the array then you would pass 3 as the second argument and so n is going to equal 3. Thus, the for loop will execute three times because i will be less than n (which is 3) for i = 0, 1, and 2. The function will mutliply arr[0] times arr[1] times arr[2] which are the first three numbers in the array.

1 Like

Okay things are a little more clear, thank you, i understands i < n now and see how that could be rewritten as i < n - 1. So how do i breakdown return multiply(arr, n - 1) * arr[n - 1]; from the recursive function to see what went on behind the scenes especially the * arr[n - 1]; bit.

The best way to do this is to use real numbers. If I give you the following:

multiply([5,6,7], 2)

You know you are going to hit the else statement because n > 0. So the return statement will be executed:

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

Replace the variables above with their actual values based on sum([5,6,7], 2).

I don`t get it. Is this grouping correct?
return multiply([5, 6, 7], 2 - 1) * arr[2 - 1];
Or
return multiply([5, 6, 7], 2) * arr[2];
using real numbers isnt helping me see * arr[n - 1]; purpose here.

You didn’t quite replace all of the variables with their actual values. Your first one is the most correct:

return multiply([5, 6, 7], 2 - 1) * arr[2 - 1];

Here you replaced n with its value, but you can go further. First of all, instead of 2-1, let’s just write what that actually is:

return multiply([5, 6, 7], 1) * arr[1];

But we still have a variable in there, the array arr. There is nothing tricky going on here. You know what the array is. It is passed into the function. So what value is stored in arr[1]?

Please rewrite this:

return multiply([5, 6, 7], 1) * ???;

Replacing ??? with the value stored in arr[1].

return multiply([5, 6, 7], 1) * 6;
Going by arrays being zero-indexed the value 6 is in index 1 position.

Correct. So what we know now is that the initial call to multiply([5, 6, 7], 2) returns multiply([5, 6, 7], 1) * 6. So the return statement is:

return multiply([5, 6, 7], 1) * 6;

But computers will keep on solving things until they can’t go any further. The function is not literally going to return another function call multiplied by 6. What do you think it is going to do?

The function is not literally going to return another function call multiplied by 6. What do you think it is going to do?

I am not sure, as that is the confusing bit since ive been thinking about it literally but now am thinking its indicating the value at index 1 as the first value of n - 1 which does not match the base case, so what it will do is store that and move on to the next n - 1 value in
return multiply([5, 6, 7], 0) * 5;
which now satisfies the base case but i am not sure.

I think you are on the right track, I’m just not sure your explanation is clear to me.

If you had a return statement of:

return Math.random() * 6;

I think (or at least hope) you would agree that the function call to Math.random() will be done first in order to get a return value from that function call, and then that return value from Math.random() will be multiplied by 6, and then the product of that multiplication will be returned by the return statement. In other words:

  • First get return value from Math.random()
  • Multiply the return value by 6
  • Return the product of that multiplication

So wouldn’t the same thing happen for a return statement of:

return multiply([5, 6, 7], 1) * 6;

In other words, the return statement needs to execute multiply([5, 6, 7], 1) in order to get a return value from it and then it can multiply that return value by 6 to return an answer.

Executing multiply([5, 6, 7], 1) is a completely separate function call (even though it shares the same name). It’s just like calling Math.random(). The return statement waits around until multiply([5, 6, 7], 1) returns a value and then it can multiply that value by 6 to return a final answer. The only “weird” thing about this is that the return statement is calling itself. But don’t let that throw you too much. It’s still basically the same thing as if it called Math.random() or any other function.

So knowing the above, and that we need to get a value from multiply([5, 6, 7], 1) before we can multiply it by 6, what does multiply([5, 6, 7], 1) return?

Been staring at it but TBH i still dont know what it returns:(

What is ‘it’? Which function call do you mean?

Don’t make this too hard. You were able to figure out above that multiply([5,6,7], 2) returns multiply([5, 6, 7], 1) * 6.

So I’m sure you can use the same reasoning to figure out what multiply([5, 6, 7], 1) returns. You know it is going to hit the else block in the function because n > 0. So it is going to execute the return statement:

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

Fill in all the actual values for the variables like we did above.

if i apply the same reasoning ‘1’ within
multiply([5, 6, 7], 1) * will be indicating an index position with value 6 as well, so it will be return 6 * 6;

Hmm, it seems like maybe you forgot what the definition of the function multiply is:

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

So let me ask again, based on the above definition, what will multiply([5, 6, 7], 1) return? Don’t worry about anything else we’ve done up to this point. It doesn’t matter. The only thing we are concerned about is solving multiply([5, 6, 7], 1) because we have to solve that before we can return a value from the original function call of multiply([5, 6, 7], 2).

My head is a turmoil of i-dont-even-know-what-am-doing-anymore, ive been looking at this multiply([5, 6, 7], 1) all day, watched youtube tutorials upon tutorials read and reread articles on recursion and read your hints line by line but i cant see what is implied to be easy to be seen here. I am so demotivated and doubting myself :frowning: . I know i am overthinking this but i cant help it. Recursion is frustrating me out of my mind and jumbled up the progress i thought i was making. :triumph:

if multiply([5, 6, 7], 1) is to return the value 5, the way i see that is if multiply([5, 6, 7], 1) hits the else statement again and drops to multiply([5, 6, 7], 0) with the ‘0’ indicating the index position of value ‘5’ in the array but i dont think that is right, beyond this i cant think of anything else.

That’s not quite what it returns. Let’s review. The original function call is multiply([5,6,7], 2). We know that returns

multiply([5, 6, 7], 1) * 6

We know this because n equals 2 inside the function and thus we will execute the return statement, which is

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

And replacing the variables with their actual values gives us multiply([5, 6, 7], 1) * 6.

So now we need to solve for multiply([5, 6, 7], 1) so that we can get a value from the recursive function call and then multiply that value by 6 so we can return a final answer. Hopefully you are with me up to this point.

Now I want you to forget about everything we have previously done up to this point. Don’t even think about this as being part of a recursive function. The only thing you need to do at the moment is tell me what multiply([5, 6, 7], 1) returns. How do you figure that out? You run it through the multiply function just like you ran multiply([5, 6, 7], 2) through the function. Again, don’t worry about trying to figure out the final answer or how it relates to the previous function call. This is a completely independent process.

So again, I ask you, what does multiply([5, 6, 7], 1) return?

it will returns multiply([5, 6, 7], 0).

You are forgetting something. Look at the return statement in the function:

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

Yes, you have the multiply(arr, n - 1) part right. But what about * arr[n - 1]?

Okay, that will be multiply([5, 6, 7], 0) * 5.