app/models/upc.rb
# Calculate the check digit (last number) based on first 11 number of the UPC code
Calculate the check digit (last number) based on first 11 number of the UPC code
Calculate the check digit ( last number ) based on first 11 number of the UPC code
def check_digit
    odd = code.each_char.each_slice(2).map(&:first).map(&:to_i).sum
    even = code.slice(0, 10).each_char.each_slice(2).map(&:last).map(&:to_i).sum
    res = (odd * 3 + even) % 10
    res = (10 - res) unless res == 0
    res
  end
def check_digit odd = code . each_char . each_slice ( 2 ) . map ( & :first ) . map ( & :to_i ) . sum even = code . slice ( 0 , 10 ) . each_char . each_slice ( 2 ) . map ( & :last ) . map ( & :to_i ) . sum res = ( odd * 3 + even ) % 10 res = ( 10 - res ) unless res == 0 res end
Calculate the check digit (last number) based on first 11 number of the UPC code
Calculate the check digit ( last number ) based on first 11 number of the UPC code