Looking for a way to solve this problem by recursing sum(). Right now, the code works, but I am supposed to call sum() more than once, and it should not mutate the input array.
var sum = function(array) {
if(array.length === 0){
return 0;
}
function add(array, i){
console.log(array[i]);
if(i === array.length-1){
return array[i];
}
return array[i] + add(array, i+1);
}
return add(array, 0);
};
sum([1, 2, 3, 4, 5, 6]) //21
function sum(array) { return array.length ? array[0] + sum(array.slice(1)) : 0; }suman extra optional parameter :-)