📜  javascript 递归求和函数 - Javascript 代码示例

📅  最后修改于: 2022-03-11 15:04:15.105000             🧑  作者: Mango

代码示例1
// 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;
}