Skip to content

Instantly share code, notes, and snippets.

@david-mateogit
Last active January 30, 2020 20:18
Show Gist options
  • Save david-mateogit/04e6052e4b7f6d62c1c0b3cff87fb053 to your computer and use it in GitHub Desktop.
Save david-mateogit/04e6052e4b7f6d62c1c0b3cff87fb053 to your computer and use it in GitHub Desktop.
Luhn’s Algorithm - Credit Card # Check.
// test cards
// https://developer.paypal.com/docs/classic/payflow/payflow-pro/payflow-pro-testing/#credit-card-numbers-for-testing
const card = 378282246310005;
let mod = 1;
let flag = true;
let sum = 0;
const digits = 16;
for (let i = 1; i <= digits; i++ ) {
let num = Math.floor((card / mod) % 10);
if ((flag = !flag)) {
num *= 2;
}
if ( num > 9) {
num -= 9;
}
mod *= 10;
sum += num;
}
if (sum % 10 === 0)
{
console.log("VALID CARD");
// here do another check for issuer;
}
else
{
console.log("INVALID CARD");
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment