Last active
January 25, 2020 07:43
-
-
Save mkxml/11214407 to your computer and use it in GitHub Desktop.
Check Digit GS1
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
/* | |
Check digit GS1 | |
Arguments: | |
- [String] GS1 code without check digit to generate | |
the check digit | |
Returns: | |
- [Number] Check digit for given code | |
Author: Matheus R. Kautzmann | |
*/ | |
var checkDigitGS1 = function(n) { | |
var l = n.length, i = 0, v = 0, p = false; | |
for(i = l-1; i >= 0; i--) { | |
if((p = !p)) | |
v += (parseInt(n[i], 10) * 3); | |
else | |
v += parseInt(n[i], 10); | |
} | |
return ((Math.ceil(v/10) * 10) - v); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment