Created
December 4, 2023 21:40
-
-
Save OtherDevOpsGene/219fc73cc6c8b02e21732c4ffa6286cc to your computer and use it in GitHub Desktop.
luhn10() implementation
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 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