Replace Loops using Recursion

Write a recursive function, sum(arr, n) , that returns the sum of the first n elements of an array arr .
this code work correctly but i’m not understand please give me explanation?

function sum(arr, n) {
// Only change code below this line
if(n <= 0){
return 0;
return sum(arr,n-1) + arr[n-1];
// Only change code above this line


Can you be more specific about what you don’t understand? Also, if you don’t understand it then how did you get it working?

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.