Skip to content

Instantly share code, notes, and snippets.

@prashcr
Last active May 6, 2016 04:28
Show Gist options
  • Save prashcr/127b9cbd6c4f978914610dd857cfbd10 to your computer and use it in GitHub Desktop.
Save prashcr/127b9cbd6c4f978914610dd857cfbd10 to your computer and use it in GitHub Desktop.
Luhn's algorithm to validate credit card numbers
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