From 212ad739d200d51525e71d1140cffb8e8cd24de5 Mon Sep 17 00:00:00 2001 From: John O'Keefe Date: Fri, 8 Dec 2023 10:46:19 -0500 Subject: [PATCH] fixed even digit bug --- main.go | 52 ++++++++++++++++++++++++++++++++++------------------ 1 file changed, 34 insertions(+), 18 deletions(-) diff --git a/main.go b/main.go index cce1d60..d0ca72d 100644 --- a/main.go +++ b/main.go @@ -43,39 +43,55 @@ func main() { // Take all the digits in even positions (except for the last one) and add to the number you got above // Divide that number by 10 and take the remainder // If the remainder is not 0, subtract it from 10 + // + // 12/8/2023: New git updates from https://www.gs1.org/services/how-calculate-check-digit-manually oddNumber := 0 evenNumber := 0 var remainder int - if upcLength < 10 { - gtins = append(gtins, "Cannot process upcs less than 10 digits") + if upcLength < 8 { + gtins = append(gtins, "Cannot process upcs less than 8 digits") continue } - if upcLength == 10 { - upc = "0" + upc + if upcLength > 17 { + gtins = append(gtins, "Invalid barcode") + continue } - if upcLength > 13 { - gtins = append(gtins, "GTINs longer than 14 digits are not valid") - } - - for i, num := range upc { - if i%2 == 0 { - oddString, err := strconv.Atoi(string(num)) - Error(err) - oddNumber += oddString - } else { - if upcLength-1 > i { - evenString, err := strconv.Atoi(string(num)) + if upcLength%2 == 0 { + for i, num := range upc { + if i%2 != 0 { + convertedOddNumber, err := strconv.Atoi(string(num)) Error(err) - evenNumber += evenString + convertedOddNumber = convertedOddNumber * 3 + oddNumber += convertedOddNumber + } else { + if upcLength-1 > i { + convertedEvenNumber, err := strconv.Atoi(string(num)) + Error(err) + evenNumber += convertedEvenNumber + } + } + } + } else { + for i, num := range upc { + if i%2 == 0 { + convertedOddNumber, err := strconv.Atoi(string(num)) + Error(err) + convertedOddNumber = convertedOddNumber * 3 + oddNumber += convertedOddNumber + } else { + if upcLength-1 > i { + convertedEvenNumber, err := strconv.Atoi(string(num)) + Error(err) + evenNumber += convertedEvenNumber + } } } } - oddNumber = oddNumber * 3 remainder = (oddNumber + evenNumber) % 10 if remainder != 0 { remainder = 10 - remainder