Skip to content

Instantly share code, notes, and snippets.

@jillbert
Created June 19, 2020 16:26
Show Gist options
  • Save jillbert/9811e1ace042cb0ffa2a5e417ca73f90 to your computer and use it in GitHub Desktop.
Save jillbert/9811e1ace042cb0ffa2a5e417ca73f90 to your computer and use it in GitHub Desktop.
p "hey there give us a CC #"
cc_number = gets.chomp
def check_type(cc_number)
type = "Unknown"
type_check = cc_number[0..1].to_i
case type_check
when 34, 37
type = "AMEX" unless cc_number.length != 15
when 51..55
type = "MasterCard" unless cc_number.length != 16
when 60
type = "Discover" unless cc_number[2..3] != "11" && cc_number.length != 16
when 40..49
type = "VISA" unless cc_number.length != 13 && cc_number.length != 16
end
type
end
def check_valid(cc_number)
valid = "invalid"
valid_check = cc_number.reverse.chars
valid_check.map!.with_index do |n, i|
if i % 2 != 0 then n.to_i * 2 else n.to_i end
end
valid_check.map! do |n|
if n > 9 then n.to_s.chars.map(&:to_i) else n end
end
valid = "valid" unless valid_check.flatten.sum % 10 != 0
valid
end
def results(type, valid, cc_number)
p "#{type}: #{cc_number} (#{valid})"
end
results(check_type(cc_number), check_valid(cc_number), cc_number)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment