Help me understand. New coder

Basic JavaScript: Replace Loops using Recursion |

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

In this problem, it states "notice that multiply(arr, n) == multiply(arr, n - 1) * arr[n - 1] "

I’m not understanding how they equal each other. I’m not “noticing” anything. For example, where in the world are the -1’s coming from?

Please post actual code and a link to the challenge you are working on. Pictures are hard to read. Thanks.

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

This says:
multiplying the first n elements of arr is the same as taking the product of the first n-1 elements of arr and multiplying that by the nth element of arr

My apologies. I’ve edited my post and know better for next time. As for your answer, I’m still a little thrown off. How do they equal each other if one is seemingly more steps than the other? Where do those -1’s come from and why are there square brackets around the second n -1?

So typing multiply(arr, n) is exactly like typing out multiply(arr, n - 1)* arr [n-1]
Am I interpreting that incorrectly?

btw, sorry if these questions seem dumb or confuse you. I’ve just started coding last week. This is my first language. Was doing alright but real confused on how these equal each other.

The produce the exact same number. Let’s look at a specific case.

const arr = [1, 2, 3, 4];
const first_way = multiply(arr, 4); // This is 1*2*3*4=24

console.log(multiply(arr, 3)); // This is 1*2*3=6
console.log(multiply(arr[3])); // This is 4
const second_way = multiply(arr, 3) * arr[3]; // This is also 6*4=24

console.log(first_way === second_way); // 'true', they provide the same value

Okay great thank you I’m definitely understanding this more. Question for
console.log(multiply(arr[3])); // This is 4

That is 4 because the number 4 is the [3]rd iteration in the array. Correct??

Remember though that arrays start counting at 0

Right, got it. So using that info in my original problem,
multiply(arr, n) == multiply(arr, n - 1) * arr[n - 1]

Since the binary count starts at 0, is the example just showing that the n-1 just doesn’t exist yet?

I 100% understand your specific case so I believe I’ve grasped what is being stated.
I think I’m just overthinking it since the original problem has arr[n-1] whereas in your example you use arr[3] which is far easier to read in my opinion.

It isn’t a ‘binary count’. Arrays start counting from zero because the index means 'how far away from the beginning '.

The example uses n-1 instead of 3 because the statement is true when n=4 or when n=2 or when n=736738263839.

1 Like

Oh yeah binary count deals with only 0’s and 1’s excuse my ignorance.

Okay I see. I was reading it way too literally. For some reason I was trying to find specifically where the -1 was coming from but am now able to see that it’s just in place to show the statement will still be true if n= 9 or n= 99999999 . You’ve been a huge help. Thank you!

1 Like

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