I’m into this challenge: Replace Loops using Recursion in Basic JavaScript
The code example in the challenge is this:
function multiply(arr, n) {
if (n <= 0) {
return 1;
} else {
return multiply(arr, n - 1) * arr[n - 1];
}
}
And I’ve coded it like this:
let arr = [1, 2, 3, 4];
function multiply(arr, n) {
if (n <= 0) {
return arr[0];
} else {
return arr[n] * multiply(arr, n - 1);
}
}
multiply(arr, arr.length - 1);
Both codes return the same value. I really wonder if there’s any subtle difference between these two?
Link to the challenge: