fixed even digit bug

This commit is contained in:
John O'Keefe 2023-12-08 10:46:19 -05:00
parent 80bcb32d4c
commit 212ad739d2

52
main.go
View File

@ -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 // 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 // Divide that number by 10 and take the remainder
// If the remainder is not 0, subtract it from 10 // 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 oddNumber := 0
evenNumber := 0 evenNumber := 0
var remainder int var remainder int
if upcLength < 10 { if upcLength < 8 {
gtins = append(gtins, "Cannot process upcs less than 10 digits") gtins = append(gtins, "Cannot process upcs less than 8 digits")
continue continue
} }
if upcLength == 10 { if upcLength > 17 {
upc = "0" + upc gtins = append(gtins, "Invalid barcode")
continue
} }
if upcLength > 13 { if upcLength%2 == 0 {
gtins = append(gtins, "GTINs longer than 14 digits are not valid") for i, num := range upc {
} if i%2 != 0 {
convertedOddNumber, err := strconv.Atoi(string(num))
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))
Error(err) 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 remainder = (oddNumber + evenNumber) % 10
if remainder != 0 { if remainder != 0 {
remainder = 10 - remainder remainder = 10 - remainder