Created
December 2, 2016 00:27
-
-
Save janderson16/7881ab72054f9d77acacc37037c88624 to your computer and use it in GitHub Desktop.
credit_check.rb
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
account_number = "5541808923795240" | |
cc_number = account_number.chars.map do |num| | |
num.to_i | |
end | |
doubled_digits = cc_number.map.with_index do |num, index| | |
if index.even? | |
num * 2 | |
else | |
num | |
end | |
end | |
sum_over_10 = doubled_digits.map do |num| | |
if num > 9 | |
num -= 9 | |
else | |
num | |
end | |
end | |
total = sum_over_10.reduce(:+) % 10 | |
if total == 0 | |
puts "Card is valid" | |
else | |
puts "Card is invalid" | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment