Hi I read articles on recursion and read the example question here, but can’t seem to understand the question enough to be able to explain it:
Write a recursive function,
sum(arr, n)
, that returns the sum of the firstn
elements of an arrayarr
.
I understand that a value minus itself is used to count down or up in a recursion, but the idea of counting n elements and using that isn’t clicking in my mind. I can solve this question, but I don’t understand how it’s
return multiply(arr, n - 1) * arr[n - 1];
if (n <= 0) {
return 1;
} else {
return multiply(arr, n - 1) * arr[n - 1];
}
}
Challenge: Replace Loops using Recursion
Link to the challenge: