Skip to content

Instantly share code, notes, and snippets.

@agodin3z
Created November 17, 2020 02:00
Show Gist options
  • Save agodin3z/f85256bc7e11fa90c9e75140ed039812 to your computer and use it in GitHub Desktop.
Save agodin3z/f85256bc7e11fa90c9e75140ed039812 to your computer and use it in GitHub Desktop.
Validate credit/debit card number (Luhn Algorithm)
export const validateCard = ((arr) => {
return (num) => {
let len = num.length;
let bit = 1;
let sum = 0;
let val;
while (len) {
val = parseInt(num.charAt(--len), 10);
// eslint-disable-next-line no-bitwise,no-cond-assign
sum += (bit ^= 1) ? arr[val] : val;
}
return sum && sum % 10 === 0;
};
})([0, 2, 4, 6, 8, 1, 3, 5, 7, 9]);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment