Skip to content

Instantly share code, notes, and snippets.

@vistun
Created September 3, 2016 04:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save vistun/c334696a10d683f31cfeddc91f13a1c4 to your computer and use it in GitHub Desktop.
Save vistun/c334696a10d683f31cfeddc91f13a1c4 to your computer and use it in GitHub Desktop.
Generate check digit for UPC-A
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