Skip to content

Instantly share code, notes, and snippets.

@netoht
Created September 13, 2011 20:20
Show Gist options
  • Save netoht/1215028 to your computer and use it in GitHub Desktop.
Save netoht/1215028 to your computer and use it in GitHub Desktop.
calculaDv
function calculaDVGtin(digits) {
if (isNaN(digits)) return null;
var code = (digits).toString(),
even = odd = 0;
for (var i = code.length - 1; i >= 0; --i) {
if ((code.length - i) % 2 == 0) {
odd += Number(code.charAt(i));
} else {
even += Number(code.charAt(i));
}
}
var check = 10 - ((even * 3 + odd) % 10);
return (check >= 10) ? 0 : check;
}
function calculaDVModulo10(digits) {
if (isNaN(digits)) return null;
var code = (digits).toString(),
sum = 0;
for (i = 0; i < code.length; ++i) {
number = Number(code.charAt(i)) * ((i % 2 == 0) ? 2 : 1);
if (number == 10) {
++sum;
} else if (number < 10) {
sum += number;
} else {
sum += number / 10 + number % 10;
}
}
return (10 - (sum % 10));
}
function calculaDVModulo11(digits) {
if (isNaN(digits)) return null;
var code = (digits).toString(),
sum = 0,
factor = 2;
for (i = code.length - 1; i >= 0; --i) {
sum += Number(code.charAt(i)) * factor;
if (++factor <= 9) continue;
factor = 2;
}
var check = 11 - (sum % 11);
return (check >= 10) ? 0 : check;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment