LoginSignup
3
3

More than 5 years have passed since last update.

JANコードのデジットをチェックする

Last updated at Posted at 2013-01-31

JANコードのチェックデジット

  • 短縮/標準のJANコードのチェックデジットを計算し、返します。
check_digit.rb
def check_digit(code)
  code = [8,13].include?(code.to_s.size) ? code[0..2].to_s : code.to_s
  exit unless [7,12].include?(code.size)
  total_even = total_odd = 0
  code.split(//).each_with_index do |x,i|
    (i % 2) == 0 ? total_even += x.to_i : total_odd += x.to_i
  end
  10 - ((total_even * 3) + total_odd) % 10
end

jan = ARGV.shift
p check_digit(jan)
3
3
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
3