Skip to content

Instantly share code, notes, and snippets.

@kmandreza
Created January 5, 2013 22:23
Show Gist options
  • Select an option

  • Save kmandreza/4464032 to your computer and use it in GitHub Desktop.

Select an option

Save kmandreza/4464032 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.
def product(x)
p = nil
x.each do |y|
if p.nil?
p = y
else
p = p * y
end
end
p
end
def product(x)
p = 1
x.each do |y|
p = p * y
end
p
end
def product(x)
x.reduce {|p,y| p * y }
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment