Skip to content

Instantly share code, notes, and snippets.

@duemir
Created March 22, 2013 12:56
Show Gist options
  • Select an option

  • Save duemir/5221029 to your computer and use it in GitHub Desktop.

Select an option

Save duemir/5221029 to your computer and use it in GitHub Desktop.
JS Luhn algorithm for node - functional style!
var luhn = function (number) {
return number.split('').map(function (digit, index, arr) {
return (index % 2 !== arr.length % 2) ? parseInt(digit, 10) : 2 * digit;
}).reduce(function sum(val, cur, idx, arr) {
return val + (cur < 10 ? parseInt(cur, 10) : (String(cur).split('').reduce(sum, 0)));
}, 0) % 10;
};
exports.is_valid = function (number) {
return luhn(number) === 0;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment