Skip to content

Instantly share code, notes, and snippets.

@OtherDevOpsGene
Created December 4, 2023 21:40
Show Gist options
  • Save OtherDevOpsGene/219fc73cc6c8b02e21732c4ffa6286cc to your computer and use it in GitHub Desktop.
Save OtherDevOpsGene/219fc73cc6c8b02e21732c4ffa6286cc to your computer and use it in GitHub Desktop.
luhn10() implementation
function luhn10(cc) {
let num = normalize(cc);
let sum = 0;
let dbl = true;
let checkDigit = num.slice(-1);
for (let i = num.length - 2; i >= 0; i--) {
if (dbl) {
let val = +num.charAt(i) * 2;
if (val > 9) {
val = Math.floor(val / 10) + (val % 10);
}
sum += +val;
dbl = false;
} else {
sum += +num.charAt(i);
dbl = true;
}
}
let total = sum + +checkDigit;
return total % 10 == 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment