1// Write a recursive method that returns the sum of all elements in an array
2
3function recSum(nums) {
4 if (nums.length === 1 ) {
5 return nums[0];
6 }
7 if (nums.length === 0 ) {
8 return 0;
9 }
10 let sum = nums[0] + recSum(nums.slice(1,nums.length));
11 return sum;
12}
1function sum(n) {
2 if (n < 1) return 0; // exit condition
3 return n + sum(n - 1); // return value plus result of recursive call
4}
5
6console.log(sum(3));