Created
December 17, 2014 06:02
-
-
Save mikedao/7693312a170b891cfd2f to your computer and use it in GitHub Desktop.
Luhn's Algorithm
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 validator(card) | |
card.chars | |
.map { |n| n.to_i } | |
.map.with_index { |n, index| index.even? ? n * 2 : n } | |
.map { |n| n.to_s.length == 2 ? n.to_s[0].to_i + n.to_s[1].to_i : n } | |
.reduce(:+) % 10 == 0 ? "Valid" : "Invalid" | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment