Write a recursive program to sum up an array.
Easily done with reduce, but more fun with recursion:
var sumMeUp = [3,4,5,6];
output should equal 18
Hint: Do you want a hint? If so, read down below:
pop it + everything else in the array.
Just keep popping.
var array_sum = function(my_array) {
ReplyDeleteif (my_array.length === 1) {
return my_array[0];
}
else {
return my_array.pop() + array_sum(my_array);
}
};