Last active
June 19, 2025 08:52
-
-
Save mityaua/6f56bd09423ee027f827f2a57fa6c36a to your computer and use it in GitHub Desktop.
Luhn Algorithm - Credit Card Number Checker
This file contains hidden or 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
const checkByLuhn = (card: string): boolean => { | |
if (!card) { | |
return false; | |
} | |
let checksum: number = 0; | |
const digits: number[] = card.split("").map(Number); | |
for (let i = 0; i < digits.length; i += 1) { | |
const digit = digits[i]; | |
if (i % 2 === 0) { | |
const doubled = digit * 2; | |
checksum += doubled > 9 ? doubled - 9 : doubled; | |
} else { | |
checksum += digit; | |
} | |
} | |
return checksum % 10 === 0; | |
}; | |
console.log(checkByLuhn("5559490000000007")); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment