Created
August 28, 2023 21:37
-
-
Save junaruga/2d9df494bed72d4a1de98b12e83530d1 to your computer and use it in GitHub Desktop.
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
$ cat card.rb | |
# Set your card number here. 16 digits. | |
card_no = '1234123412341234' | |
reverse_card_no = card_no.reverse | |
index = 1 | |
total = 0 | |
reverse_card_no.each_char do |c| | |
multi = (index % 2 == 1) ? 1 : 2 | |
sub_total = c.to_i * multi | |
sub_total -= 9 if sub_total >= 10 | |
total += sub_total | |
index += 1 | |
end | |
puts "total: #{total}" | |
if total % 10 == 0 | |
puts "Valid card no." | |
else | |
puts "Invalid card no." | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment