Pages

Monday, August 29, 2016

JS recursive exercise 4

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.

1 comment:

  1. var array_sum = function(my_array) {
    if (my_array.length === 1) {
    return my_array[0];
    }
    else {
    return my_array.pop() + array_sum(my_array);
    }
    };

    ReplyDelete