function sum(arr, n) {
// Only change code below this line
if(n <= 0) {
return 0; // In the base case, where n <= 0, it returns 1.
} 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 6.3; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/84.0.4147.105 Safari/537.36.
That’s the base case; n == 0 means you want the sum of the first zero elements of the array, in which case the sum is 0 so you return 0. If n is negative, that also should return 0 because you can’t provide a sum of a negative number of elements. Without a base case, the recursion would go on forever since in each step the value of n is decreasing.