Skip to content

Instantly share code, notes, and snippets.

@mkxml
Last active January 25, 2020 07:43
Show Gist options
  • Save mkxml/11214407 to your computer and use it in GitHub Desktop.
Save mkxml/11214407 to your computer and use it in GitHub Desktop.
Check Digit GS1
/*
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