Skip to content

Instantly share code, notes, and snippets.

@mcshakes
Created December 17, 2014 05:50
Show Gist options
  • Save mcshakes/095a52aa660b0acc9213 to your computer and use it in GitHub Desktop.
Save mcshakes/095a52aa660b0acc9213 to your computer and use it in GitHub Desktop.
credit_check
card_number = "4929735477250543"
separate_nums = card_number.chars.map(&:to_i).reverse
initial_collection = separate_nums.each_with_index.map do |digit,index|
if index.odd?
digit *= 2
else
digit
end
end
total = initial_collection.map do |num|
if num > 9
num.to_s.split("").map {|digit| digit.to_i}.reduce(:+)
else
num
end
end
if total.reduce(:+) % 10 == 0
"The number is valid!"
else
"The number is invalid"
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment