Created
December 31, 2012 20:22
-
-
Save kmandreza/4422463 to your computer and use it in GitHub Desktop.
Write a method total which takes an Array of numbers as its input and returns their total (sum). For example, total([1,2,3]) # => 6
total([4.5, 0, -1]) # => 3.5
total([-100, 100]) # => 0
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def total(array) | |
total = 0 | |
array.length.times do |i| | |
total = total + array[i] | |
end | |
total | |
end | |
puts total([1,2,3]) # => 6 | |
puts total([4.5, 0, -1]) # => 3.5 | |
puts total([-100, 100]) # => 0 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment