Created
September 28, 2018 16:46
-
-
Save danlynn/0368017adc38d468ff9cac4e5c4a779c to your computer and use it in GitHub Desktop.
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 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