Replace Loops using Recursion -1 explanation

I think I understand your explanation up until your final point.

n only = 4 because thats how you’ve called the function. How does that affect the fact that multiply(arr, n) == multiply(arr, n - 1) * arr[n - 1]?

In the explanation given in the challenge there is no array given, I’m confused as to whether you’re giving a generic explanation or specific to the challenge given.

It’s both. The exact same logic works for every size array. For every single array the product of every single value in the array is the same as the product of all but the last value and the last value.

Put symbolically

arr[0]  * arr[1] * ... * arr[n - 1] == (arr[0] * arr[1] * ... arr[n - 2]) * arr[n - 1]
1 Like

This is not a thread about radio buttons. I recommend you use the “get help” button on the challenge and start a new thread.

THANKS

Thanks so much for your help

So are you saying that the above could be this? :

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

Not quite. In the function multiply(arr, n), you are multiplying n elements of the array arr together. Keep in mind that the nth element is at index n - 1, or in other words the nth element of the array is arr[n - 1], because of 0 based indexing.

That means that multiplying n elements can be computed by multiply(arr, n) or multiply(arr, n - 1) * arr[n - 1].

1 Like

Oh I see, you mean the post where you included n-2 was just a symbolic example and not something to be compared with actual code? I got a bit confused by that! Thank you

No worries, I can see how it could be confusing. I definitely meant that line as a general example of what values need to be multiplied rather than code that can actually be evaluated.

1 Like

I had the same confusion. I think I resolved it for myself and perhaps it will help you. Part of the problem I had was the small numbers in the example array were making it less obvious. If the array were something like [12, 24, 4, 15] it is easier to see. The multiply function multiplies the number of elements you specify. So if n = 2 it multiplies the first two elements which are 12 and 24. n does not represent the index of the element it just says how many you start with. The index of the element is n-1 because the indexing is zero based. So first you have to see how many elements will be involved then you have to perform the multiplication.

The other thing that helped me was looking at what happens if you try to multiply an empty array. You don’t get 1 returned because the empty array is not a number.

Hope that helps.

1 Like

Thank you for the break down.

1 Like

In this function

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

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

how is it that you can multiply an arguments function by a position in an array?
i mean i understand that calling it like this

let arr = [7,8,9]
let n = 2
multiply( arr ,  n -1) * arr[n-1] // (where n-1 is 8)

what i dont understand is if thats a short syntax because * arr[n - 1];
because in my head is something like this where n = 2
multiply([ 7, 8 , 9] , 1 ) * 8
but whats the output or the multiply order in these cases ?
i mean i never seen it before in examples that i cant multiply a functions with arguments just like that, can someone explain in a more detail way this please ?

If it helps, mentally add ()s.

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

You are multiplying the result of the function call for the first n-1 elements by the nth element in the array.

yeah but how the value is store in array arr
like in other examples when you make an arithmetic operation you do something like

let array =[ 7,8,9]
let product =1;
let n = 2
product = product * array[n-1] // 1 * 8

so in this case the multiplication would be something like

array = array * array[n-1] ?

that is the specific part i dont understand.

You are not multiplying by the entire array.

You are multiplying multiply(arr, n - 1) and arr[n - 1]. Both of these values are numbers.

multiply(arr, n - 1) is a single number. The function call is evaluated and the return value is used in the computation.

From your example above,

multiply([ 7, 8 , 9] , 1 ) * arr[1]

becomes

multiply([ 7, 8 , 9] , 1 ) * 8

becomes

7 * 8

becomes

56

and that is the value that is returned.

In the last part
multiply([ 7, 8 , 9] , 1 )
How does this evaluates to the single number 7 ? I’m just missing that part.
Now I understand this is the value returned by function but I cant imagine how the function call end up returning position [0] of the arr or 7 in this case. Thanks a lot for your effort and time JeremyLt, I really appreciate it

Well, lets expand a bit:

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

let testArr = [7, 8, 9];
console.log(multiply(testArr, 3)); // Logs 504

This is our test case.

We are calling multiply([7, 8, 9], 3). (1st function call)

In this function call, n = 3, so we are in the else branch, and we need to compute multiply([7, 8, 9], 2) * 9).

We call multiply([7, 8, 9], 2). (2nd function call)

In this function call, n = 2, so we are in the else branch, and we need to compute multiply([7, 8, 9], 1) * 8).

We call multiply([7, 8, 9], 1). (3rd function call)

In this function call, n = 1, so we are in the else branch, and we need to compute multiply([7, 8, 9], 0) * 7).

We call multiply([7, 8, 9], 0). (4th function call)

In this function call, n = 0, so we return 1.

Now back to our previous function call.

We can now return 1 * 7. (3rd function call)

Now back to our previous function call.

We can now return 7 * 8. (2nd function call)

Now back to our previous function call.

We can now return 56 * 9. (1st function call)

This was the original function call. We have returned 504 and logged the value to the console.

1 Like

Holy god :exploding_head:
Now I see that is not the most natural thing to understand, but your explanation was perfect. Thanks a lot Jeremy! You just motivate me to keep going I had 2 days figuring this out.

1 Like

Recursion is a bit of a brain bender. I’m glad could help :slight_smile:

Hi : ) i dont know how to send this request for helping future students but, i figure it out that this lesson was almost impossible to understand without acknowledge of the call stack wich nobody talks about it or event mention its exist, i know its implicit in the lesson but that may be a reason why people stuck here, (was my case) until i double check with your replies and some other content on MDN and youtube, i hope you can pass this feedback to the team and hopefully this wont be taken the wrong way with me giving feedback when not requested, anyway i leave a video that help me fully understand call stack in case someone can see this Recursion Crash Course - YouTube , thank you for your time Jeremy [ :

1 Like