Created
January 12, 2013 04:20
-
-
Save kmandreza/4516064 to your computer and use it in GitHub Desktop.
Write a method called product which takes as its input an array of integers and returns their product. For example product([1,2,3]) # returns 6
product([0,-1,-10]) # returns 0
product([1,-1,-10]) # returns -11
If you need to iterate over the array, please use Array#each. Don’t use, e.g., inject.
This file contains 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 product(x) | |
p = 1 | |
x.each do |y| | |
p = p * y | |
end | |
p | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment