1//Write a recursive function, sum(arr, n), that returns the sum of the first n elements of an array arr.
2 /* 1) sum([1], 0) should equal 0.
3 2) sum([2, 3, 4], 1) should equal 2.
4 3) sum([2, 3, 4, 5], 3) should equal 9. */
5
6function sum(arr, n) {
7 if(n <= 0) {
8 return 0;
9 } else {
10 return sum(arr, n - 1) + arr[n - 1];
11 }
12 }
13
14