programming-examples/js/Recursion/Write a JavaScript program to compute the sum of an array of integers..js

10 lines
228 B
JavaScript
Raw Normal View History

2019-11-15 12:59:38 +01:00
var array_sum = function(my_array) {
if (my_array.length === 1) {
return my_array[0];
}
else {
return my_array.pop() + array_sum(my_array);
}
};
console.log(array_sum([1,2,3,4,5,6]));