Tell us what’s happening:
Hi
So I’m trying to get to grips with the code used for recursions.
I don’t even have enough of an understanding to write this code out if left it for an hour and came back.
So below in the code: if( n <= 0 ) {
return 0}
How does zero indexing work here? Is 0 the first element of the array?
Is the code running through the array backwards?
**Your code so far**
function sum(arr, n) {
// Only change code below this line
if(n <= 0) {
return 0;
}else{
return sum(arr, n-1)+arr[n-1]
}
}
// Only change code above this line
**Your browser information:**
User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/89.0.4389.90 Safari/537.36 Edg/89.0.774.57
.
Challenge: Replace Loops using Recursion
Link to the challenge: