Recursion replacing a for loop?

Hey eveyone,

I’m having a hard time understanding recursion. The explanation shows multiply(arr, n - 1) * arr[n - 1] as example a way to rewrite :

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

but I don’t get how ‘’ multiply(arr, n - 1) ‘’ can be used as an element to be multiplied to " arr[n - 1]". Isn’t it the name of the function and the parameters? How could it be used to multiply something?

Thank you so much for the help!

multiply(arr, n - 1) is a function call. This function call will return a number, which is the product of the first n - 1 numbers in your array.

1 Like

But how can I know that the function call represents that? If it’s not explicit in the function body? I can’t understand still :grimacing:

It is explicit (so long as you speak JavaScript, which is hard : ) ) in the function body. The recursive version of the function says

return 1;

in the base case, and that’s a number. In the other case you have

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

which has to also be a number since arr[n - 1] is a number and the base case of multiply(arr, n - 1) is a number.

1 Like

Thanks for your help, truthfully! I’m still learning as you can notice hahah And unfortunately I still don’t get it :confused: I understand that arr[n - 1] is a number, but multiply(arr, n - 1) keeps being a mystery to me.

One of two things happens when we call multiply(arr, n).

Case 1: n < 1 and we return 1
Case 2: n >= 1 and we return multiply(arr, n - 1) * arr[n - 1]

In the first case, we return a number.

In the second case, we have a number multiplied by the result of multiply(arr, n - 1), but that will also be a number.

Lets, see an example:

We call
multiply([2, 5, 4], 3)

We are in Case 2, so we want to return multiply([2, 5, 4], 2) * 4. But we don’t know what multiply([2, 5, 4], 2) is!

We call
multiply([2, 5, 4], 2)

We are in Case 2, so we want to return multiply([2, 5, 4], 1) * 5. But we don’t know what multiply([2, 5, 4], 1) is!

We call
multiply([2, 5, 4], 1)

We are in Case 2, so we want to return multiply([2, 5, 4], 0) * 2. But we don’t know what multiply([2, 5, 4], 0) is!

We call
multiply([2, 5, 4], 0)

We are in Case 1, so we return 1.

Now we know multiply([2, 5, 4], 0) == 1.

Now we know multiply([2, 5, 4], 1) returns multiply([2, 5, 4], 0) * 2 == 1 * 2 == 2

Now we know multiply([2, 5, 4], 2) returns multiply([2, 5, 4], 1) * 5 == 2 * 5 == 10

Now we know multiply([2, 5, 4], 3) returns multiply([2, 5, 4], 2) * 4 == 10 * 4 == 40

4 Likes

Thanks Jeremy for your explanation. I have the same doubt as her about the recursion. In the example first we are calling
multiply([2, 5, 4], 3), here the n = 3. But below you are calling n=2. I got confused there…

inside the function multiply([2, 5, 4], 3) you have the line return multiply([2, 5, 4], 2) * 4

sorry i did not understand you. Below the function returns a ONE if the n is less than or equal to zero. but if its not, then it calls itself again but with n - 1 and its been multiplied by arr[n-1]. the arr[n-1] will give a number to be multiplied with, that i understood. But how does the function multiply(arr, n-1) returns a number ? the only way to return a number is if n is less than or equal to 0.

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

and inside multiply([2,5,4],2) you have return multiply([2,5,4],1) * 5

inside this last one you have return multiply([2,5,4], 0) * 2

inside this last one you finally have return 1

now each thing that was waiting for a value will get one starting from the last one

1 Like

thanks for the support, im still trying to understand it though. im going to do the 108 lesson which is using recursion to create a countdown. I dont know how it will go, but ill come back to you if do not pass the test cases.
Thank you

@JeremyLT there are no words to thank you for your exhaustive explanation and the time you have put into it. You are the man!

1 Like