Last active
May 6, 2016 04:28
-
-
Save prashcr/127b9cbd6c4f978914610dd857cfbd10 to your computer and use it in GitHub Desktop.
Luhn's algorithm to validate credit card numbers
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
function validate(n){ | |
return n.toString().split('').map(Number).reduceRight(function (prev, digit, index, arr) { | |
if ((arr.length - index) % 2 === 0) { | |
return prev + (digit * 2).toString().split('').map(Number).reduce((a, b) => a + b) | |
} | |
else { | |
return prev + digit | |
} | |
}, 0) % 10 === 0 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment