Created
November 17, 2020 02:00
-
-
Save agodin3z/f85256bc7e11fa90c9e75140ed039812 to your computer and use it in GitHub Desktop.
Validate credit/debit card number (Luhn Algorithm)
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
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