Created
January 12, 2013 04:21
-
-
Save kmandreza/4516065 to your computer and use it in GitHub Desktop.
Write a method called product_odd which takes as its input an array of integers and returns the product of all the odd integers in the array. Remember that technically 0 is an even integer. For example: product_odd([1,2,3]) # returns 3, because 2 is even
product_odd([0,-1,-10]) # returns -1, because 0 and -10 are even
product_odd([1,2,3,4,5]) # …
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_odd(x) | |
odd = x.select {|y| y%2 == 1} | |
p = 1 | |
odd.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