The is what is written in the code camp challenge:
factorial(x) = factorial(x-1) * (x-1) or 4 *3* 2*1 * 4 = 96 which is incorrect.
No, you are confusing the number with the index.
Let’s think mathematically, not code. Let’s assume 1-indexing.
We have a Product function that takes a set and multiplies its elements.
Product(our set) = Product(our set without the last element) * (our set's last element).
(again, this is math, not code)
If we create a Set function that will accept return either a range (two parameters) or a value in a set (one parameter).
So, if there are 5 elements, we could rewrite this as:
Product(Set(1, 5)) = Product(Set(1, 4)) * Set(5)
OK, let’s make it more generic, n is the number of elements.
Product(Set(1, n)) = Product(Set(1, n-1)) * Set(n)
Now, rather than our Set function, we just have a set called set as the first parameter to Product and how many elements to multiply as the second:
Product(set, n) = Product(set, n -1) * (last element of set)
So if we switch back to JS, and we think of set as an array_, how do you get the last element of an array? It’s length minus 1.
multiply(arr, n) === multiply(arr, n -1) * arr[n - 1]
Again, the first n - 1
is referring to a length. If n is 5, then it is referring to the length of the array, 5. So it is telling the next iteration to reduce that by one. But as the array index, n - 1
is not reducing anything. It is referring to the nth element of the array, because it is 0-indexed.
But if you don’t believe me, just test it yourself. Try this code:
function multiply(arr, n) {
if (n <= 0) {
return 1;
} else {
return multiply(arr, n - 1) * arr[n - 1];
}
}
console.log(multiply([1, 2, 3, 4, 5], 3))
// 6
If I change it to the code that you are suggesting, it will not work anymore. It
gives the wrong answers. That is the most important test - what works. Put in some log statements and see what is happening. Pay very close attention to what arr[n - 1]
is.
Recursion is confusing. 0-indexing often trips people up.
Actually, now that I think about it, arr[n]
can’t be right. If n is the size of the array, that value will be undefined
(or garbage). When I run that, I get NaN
as the final value.