Toby1
April 6, 2020, 1:57pm
22
No, I don’t think those two are the same, why would I? I don’t understand the question.
The question I am asking is why does multiply(arr, n) == multiply(arr, n - 1) * arr[n - 1]???
To try and make it simple:
This is one thing: multiply(arr, n)
This is another thing: multiply(arr, n - 1)
Why are they the same thing. ELI5
Toby1
April 6, 2020, 2:00pm
23
I don’t think I have a problem with indexing. I’ve just gone back and done those challenges and I have no problems.
Toby1
April 6, 2020, 2:00pm
24
Perhaps you could explain which concepts you think I’ve missed?
Those two loops in my snippets are using different indexing to do the exact same thing.
Toby1:
This is one thing: multiply(arr, n)
This is another thing: multiply(arr, n - 1)
Why are they the same thing. ELI5
Nowhere is it being claimed that they are the same. Recursion depends upon them not being the same.
Toby1
April 6, 2020, 2:08pm
26
Are we looking at the same challenge?
Sure
multiply(arr, n) == multiply(arr, n - 1) * arr[n - 1]
does not mean that
Toby1:
This is one thing: multiply(arr, n)
This is another thing: multiply(arr, n - 1)
Why are they the same thing. ELI5
multiply(arr, n)
is a function call that returns the product of the first n
elements in arr
.
multiply(arr, n - 1)
is a function call that returns the product of the first n - 1
elements in arr
.
Thus, multiply(arr, n - 1) * arr[n - 1]
must be the product of the first n
elements in arr
.
Toby1
April 6, 2020, 2:21pm
29
What? How does it not mean that?
multiply(arr, n)
and multiply(arr, n - 1)
have different arguments. They should in general be different.
The statement says
multiply(arr, n) == multiply(arr, n - 1) * arr[n - 1]
This means that
multiply(arr, n)
and
multiply(arr, n - 1) * arr[n - 1]
are equivalent.
Toby1
April 6, 2020, 2:28pm
31
How is it a function call that returns the product of the first n elements in arr? Surely it’s a function declaration with two parameters?
I don’t think you’re getting what I’m asking. I don’t know how else to ask.
multiply()
is a function. multiply(arr, n)
returns a number which is the product of the first n
entries of arr
.
Function declaration:
function multiply(arr, n) {
if (n <= 0) {
return 1;
} else {
return multiply(arr, n - 1) * arr[n - 1];
}
}
Function call:
multiply(arr, n)
Thus,
multiply(arr, n) == multiply(arr, n - 1) * arr[n - 1]
means
theValueReturnedByMultiplyArrN == theValueReturnedByMultiplyArrNMinus1 * arr[n -1]
nowhere does the challenge say that theValueReturnedByMultiplyArrN
and theValueReturnedByMultiplyArrNMinus1
are the same.
Toby1
April 6, 2020, 2:51pm
33
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 n
th 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]
Toby1
April 8, 2020, 3:11pm
35
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 ?
Toby1
April 8, 2020, 3:17pm
37
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
Toby1
April 8, 2020, 3:46pm
39
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.