Am i understanding the lesson: "Replace Loops using Recursion" correctly?

Tell us what’s happening:
I am trying to understand the example provided in the lesson " Replace Loops using Recursion" , how does the recursion run the same result as the for loop. Below I included the recursion code with my understanding of how the code runs. Can someone tell me if I am understanding the steps correctly?

  **Your code so far**
    function multiply(arr, n) {
    if (n <= 0) {
      return 1;
    } else {
      return multiply(arr, n - 1) * arr[n - 1];
      /*1a. n=3, multiply(arr, 3-1=2) * arr[2], run function with n-1, n has new value as 2
      1b. n=2, multiply(arr, 2-1=1) * arr[1] * arr[2], run function with n-1, n has new value as 1
      1c. n=1, multiply(arr, 1-1=0) * arr[0] * arr[1] * arr[2], run function with n-1, n has new value as 0
      1d. n=0, multiply(arr, 0) = 1
      2a. excute 1a to 1d in reverse order, 1 * arr[0] * arr[1] * arr[2] = 1*2*3*4 = 24
      3. multiply([2, 3, 4], 3) returns 2a, which is 24*/
  multiply ([2,3,4], 3)
  **Your browser information:**

User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_6) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/14.0.3 Safari/605.1.15

Challenge: Replace Loops using Recursion

Link to the challenge:

1 Like

You have an excellent understanding of recursion. Good job.

1 Like

Still confused over here…

So does the function work precisely because you cannot multiply – ([2,3,4], 2)*4 – in the given test case, first pass through the function, so the – *4 – is left behind and – ([2,3,4],2) – is returned and the function is run agian?

This causes a stacking of multipliers – *4 * 3 * 2 – until the variable set itself in cancelled out by the “if” statement, leaving – 1 * 2 * 3 * 4 = 24 --?

If not, could you explain the order of operations that leads us to the product 24?

Thank you!

There is no ‘left behind’. When you call multiply(), the function always returns a number. Always.

The recursive function call is resolved and becomes a number, and then the product of the result from the recursive call and the current matrix entry is returned.

Okay, but how does the function know that ‘n’ equals the index of the array item being considered? Is it because “arr[n-1]” assigns it as the index representing variable?

n is the function argument. It is provided when the function is called. The function doesn’t know anything about n.

Hmmm… Let me try to rephrase the spirit of what I am seeking then.

Something inside the curly brackets is telling the function that “n” represents each index in the array. Is the thing that tells the function to behave in this way “arr[n-1]”?

Nope. This is not at all true. Absolutely nothing inside of this code ‘tells’ the function that n represents an index. It does not.

When I call

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

I’m saying that I want the product of the first 3 elements of the array [2, 3, 4].

Inside of this function call, the function will make the function call multiply([2, 3, 4], 2), which says it is taking the product of the first 2 elements of the array.

In neither case is n an index. It is the number of elements you want to multiply together.

We use the number n to determine how we are going to multiply those elements together. Specifically, we compute the product of the nth element and the product of the previous n - 1 elements.

Okay. And thank you for answering my questions.

I am however still confused about the usage of the square brackets in “arr[n-1]”. :frowning:
This arrangement sometimes references an index – “arr[n}”.

Can you tell me in pseudo-code how “arr[n-1]” is read in this instance?

arr[n - 1] is the element at index n - 1. The [n - 1] part is an index, but n is not an index. n is a number of entries to multiply together. This may seem pedantic, but code is inherently pedantic.

1 Like

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