Created
December 2, 2016 14:00
-
-
Save JonathanSR/094b5bef2b25870decffb1e754d104c0 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
#Valid: 5541808923795240, 4024007136512380, 6011797668867828 | |
#Invalid: 5541801923795240, 4024007106512380, 6011797668868728 | |
arr = [] | |
arr1 = [] | |
card_number = "6011797668867828" | |
card_number.split('') | |
arr << card_number[0] | |
arr << card_number[2] | |
arr << card_number[4] | |
arr << card_number[6] | |
arr << card_number[8] | |
arr << card_number[10] | |
arr << card_number[12] | |
arr << card_number[14] | |
arr1 << card_number[1] | |
arr1 << card_number[3] | |
arr1 << card_number[5] | |
arr1 << card_number[7] | |
arr1 << card_number[9] | |
arr1 << card_number[11] | |
arr1 << card_number[13] | |
arr1 << card_number[15] | |
arr1 = arr1.map do |num| | |
num.to_i | |
end | |
arr3 = arr.map do |num| | |
num.to_i * 2 | |
end | |
arr4 = arr3.map do |num| | |
if num > 9 | |
num - 9 | |
else | |
num | |
end | |
end | |
arr5 = (arr1 << arr4).flatten | |
sum = arr5.inject(:+) | |
if test = sum % 10 == 0 | |
then p "The number is valid!" | |
else p "The number is invalid" | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment