Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR JAVASCRIPT

javascript recursive sum function

// Write a recursive method that returns the sum of all elements in an array

function recSum(nums) {
    if (nums.length === 1 ) {
        return nums[0];
    }
    if (nums.length === 0 ) {
        return 0;
    }
    let sum = nums[0] + recSum(nums.slice(1,nums.length));
    return sum;
}
 
PREVIOUS NEXT
Tagged: #javascript #recursive #sum #function
ADD COMMENT
Topic
Name
9+6 =