Created
December 13, 2013 02:42
-
-
Save brittanmcg/7939106 to your computer and use it in GitHub Desktop.
Not quite sure why this will not raise an error when the number passed in is > or < 16.
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
class CreditCard | |
def initialize(nums) | |
@nums = nums | |
end | |
def check_card | |
raise ArgumentError unless @nums.length == 16 | |
s1 = s2 = 0 | |
@nums.to_s.reverse.chars.each_slice(2) do |odd, even| | |
s1 += odd.to_i | |
double = even.to_i * 2 | |
double -= 9 if double >= 10 | |
s2 += double | |
end | |
(s1 + s2) % 10 == 0 | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment