Last active
December 25, 2015 15:08
-
-
Save suras/6995595 to your computer and use it in GitHub Desktop.
ISBN13 barcode check digit
This file contains hidden or 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 IsbnCalculator | |
def self.isbn(num) | |
total = multiply_by(num, 1, 3) | |
mod_val = total % 10 | |
mod_val = 10 - mod_val | |
final_val = mod_val == 10 ? 0 : mod_val | |
num.to_s + final_val.to_s | |
end | |
def self.multiply_by(num, first, second) | |
tot = 0 | |
num.to_s.split("").each_with_index do |n, index| | |
if index.even? | |
tot += (n.to_i*first.to_i) | |
else | |
tot += (n.to_i*second.to_i) | |
end | |
end | |
tot | |
end | |
end | |
puts "Type a number to calculate check digit of an ISBN13 barcode" | |
num = gets.chomp | |
puts "The isbn13 check digit is" | |
puts IsbnCalculator::isbn(num) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment