Skip to content

Instantly share code, notes, and snippets.

@mityaua
Last active June 19, 2025 08:52
Show Gist options
  • Save mityaua/6f56bd09423ee027f827f2a57fa6c36a to your computer and use it in GitHub Desktop.
Save mityaua/6f56bd09423ee027f827f2a57fa6c36a to your computer and use it in GitHub Desktop.
Luhn Algorithm - Credit Card Number Checker
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