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
// 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