Skip to content

Instantly share code, notes, and snippets.

@janderson16
Created December 2, 2016 00:27
Show Gist options
  • Save janderson16/7881ab72054f9d77acacc37037c88624 to your computer and use it in GitHub Desktop.
Save janderson16/7881ab72054f9d77acacc37037c88624 to your computer and use it in GitHub Desktop.
credit_check.rb
account_number = "5541808923795240"
cc_number = account_number.chars.map do |num|
num.to_i
end
doubled_digits = cc_number.map.with_index do |num, index|
if index.even?
num * 2
else
num
end
end
sum_over_10 = doubled_digits.map do |num|
if num > 9
num -= 9
else
num
end
end
total = sum_over_10.reduce(:+) % 10
if total == 0
puts "Card is valid"
else
puts "Card is invalid"
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment