Replace Loops using Recursion -1 explanation

Your line ‘Thus’

This is the step I can’t follow! Why are they equal? !

:cry: :cry: :cry: :cry: :cry: :cry: :cry: :cry:

multiply(arr, n) computes the product of the first n elements in arr.

multiply(arr, n - 1) computes the product of the first n - 1 elements in arr.

arr[n - 1] is the nth element in arr.

So, multiply(arr, n - 1) * arr[n - 1] is the product of the first n elements in arr and

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

Hi - please read through the comments above before repeating the same explanation that didn’t help before.

I did read the comments. I’m answering your question.

Let me start at the absolute beginning. Do you understand the distinction between a function definition and the result you get from calling the function?

Yes. Apparently my post has to be 20 characters so I’m padding it out a bit.

Ok.

Let’s look at the array arr = [2, 4, 1, 5].

When I call multiply(arr, 4), I should get 2 * 4 * 1 * 5 = 40.

When I call multiply(arr, 3), I should get 2 * 4 * 1 = 8.

We can write this as
multiply(arr, 4) == 40
and
multiply(arr, 3) == 8

Note that we are using == to mean that the left and right hand side of the comparator evaluate to the same value.

Notice that
40 == 8 * 5
and that
arr[3] == 5

This means that
40 == 8 * arr[3]

We are close here.

Now we can say that
40 == multiply(arr, 3) * arr[3]

Finally, we can say that
multiply(arr, 4) == multiply(arr, 3) * arr[3]

The last thing to notice is that n = 4 so
multiply(arr, n) == multiply(arr, n - 1) * arr[n - 1]

All of this was based on the return values of these functions.

3 Likes

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.