Skip to content

Instantly share code, notes, and snippets.

@danlynn
Created September 28, 2018 16:46
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 danlynn/0368017adc38d468ff9cac4e5c4a779c to your computer and use it in GitHub Desktop.
Save danlynn/0368017adc38d468ff9cac4e5c4a779c to your computer and use it in GitHub Desktop.
function CalcCheckDigit(strMsg){
// calculate the check digit - note UPCE and UPCA check digits are the same
var Check = 0; // initialize the check digit value
for(var X = 1; X <= 11; X++){
var Test = strMsg.substr(X-1, 1);
if (isOdd(X)==true){
Check = Check + parseInt(Test) * 7; // odd position digits multiplied by 7
}
else{
Check = Check + parseInt(Test) * 9; // even position digits multiplied by 9
}
}
Check = (Check % 10) + 48; // convert value to ASCII character value;
return charFromCharCode (Check); // check character
}
function charFromCharCode (charCode) {
return unescape('%' + charCode.toString(16));
}
function isEven(y) {
return (y%2)?false:true;
}
function isOdd(y) {
return !isEven(y);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment