Created
September 3, 2016 04:27
-
-
Save vistun/c334696a10d683f31cfeddc91f13a1c4 to your computer and use it in GitHub Desktop.
Generate check digit for UPC-A
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function checkDigit(bc) { | |
while (bc.length < 11) { | |
bc = "0"+bc; | |
} | |
if (bc.length > 11) { | |
return bc; | |
} | |
var osum = 0; | |
var esum = 0; | |
for (var i = 0; i < bc.length; i+=2) { | |
osum = osum+parseInt(bc[i]); | |
} | |
for (var j = 1; j < bc.length; j+=2) { | |
esum = esum+parseInt(bc[j]); | |
} | |
var checkZero = (((osum*3) + esum) % 10); | |
if (checkZero !==0) { | |
var check = (10-checkZero).toString(); | |
return bc+check; | |
} else { | |
return bc+"0"); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment