Last active
September 26, 2018 20:56
-
-
Save amyhenning/ee184bf476aa47ba6c6a43f62ef3453f to your computer and use it in GitHub Desktop.
Created a method to determine if a credit card number is valid using the Luhn 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
module Luhn | |
def self.is_valid?(number) | |
# split the number into an array of its individual digits | |
numbers = number.to_s.split('').map { |x| x.to_i } | |
# set the index value for the starting number to be the second to last digit in numbers | |
digit_to_double = numbers.length - 2 | |
# until digit_to_double reaches the end of the array | |
while digit_to_double >= 0 | |
# double the number with an index of digit_to_double | |
doubled = numbers[digit_to_double] * 2 | |
# if the doubled number at the given index is greater than or equal to 10, subtract 9 | |
if doubled >= 10 | |
numbers[digit_to_double] = doubled - 9 | |
# otherwise, keep the doubled number | |
else | |
numbers[digit_to_double] = doubled | |
end | |
# reset digit_to_double to be the digit two to the left | |
digit_to_double -= 2 | |
end | |
# sum the numbers in the array called numbers. If it's evenly divisible | |
# by 10, it's valid. Otherwise, it's not. | |
if numbers.inject(:+) % 10 == 0 | |
return true | |
else | |
return false | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment