Skip to content

Instantly share code, notes, and snippets.

@andrewdwooten
Created December 2, 2016 06:27
Show Gist options
  • Save andrewdwooten/24b21b487baa233c229fcd3fa5ead419 to your computer and use it in GitHub Desktop.
Save andrewdwooten/24b21b487baa233c229fcd3fa5ead419 to your computer and use it in GitHub Desktop.
Module 1: Project 1: Credit Check
account_number = []
while account_number.length < 15
puts "Enter an account number to validate."
account_number = gets.chomp
end
single_digits = []
account_number.each_char {|number| single_digits << number.to_i}
#iterates through account_number and converts digits to integers and adds them to single_digits
doubled_digits = []
if account_number.length.even? then doubled_digits = single_digits.each_with_index.map { |number, index| if index.even? then number * 2 else number end}
else doubled_digits = single_digits.each_with_index.map { |number, index| if index.odd? then number * 2 else number end}
end
# multiplies alternating digits by 2
split_numbers = []
split_numbers = doubled_digits.map {|number| if number >= 10 then number.to_s.split('') else number end}
#converts numbers greater than 9 to strings and splits digits
combined_numbers = []
a = nil
b = nil
combined_numbers = split_numbers.each_with_index.map {|number, index| if split_numbers[index].class == Array then
a = number.first.to_i ; b = number.last.to_i ; split_numbers[index] = (a+b) else number end}
#digging into nested arrays and combining values
total = 0
combined_numbers.each {|number| total += number}
#calculating total of array elements
if total % 10 == 0
puts "The number is valid"
else
puts "The number is invalid"
#final calculation and output
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment