So, can someone please explain how this solves the problem?
I don't understand how it is getting #array.sum, by calling itself
and just repeating #array[0] + sum_array(array[1..-1]). I'm aware it works itself down until the array == 0 , and then work's itself back??
Can someone trace me threw the steps?
Example:
# sum_array([]) # => 0
# sum_array([5]) # => 5
# sum_array([5, 2]) # => 7
# sum_array([4, 10, -1, 2]) # => 15
#??################################??#
def sum_array(array)
return 0 if array.empty?
array[0] + sum_array(array[1..-1])
end
#??###############################??#