function sum(numbers) {
if(numbers.length == 0) {
return 0;
}
return numbers[0] + sum(numbers.slice(1));
}
this is the recursive version. there's no mutation, but we are making a call stack as below which uses extra memory.
sum([10, 5, 6, 7]);
10 + sum([5, 6, 7]);
10 + 5 + sum([6, 7]);
10 + 5 + 6 + sum([7]);
10 + 5 + 6 + 7 + sum([]);
10 + 5 + 6 + 7 + 0;