Skip to content

Instantly share code, notes, and snippets.

@kmandreza
Created December 31, 2012 20:22
Show Gist options
  • Save kmandreza/4422463 to your computer and use it in GitHub Desktop.
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
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