Skip to content

Instantly share code, notes, and snippets.

@johnfmorton
Created October 20, 2010 15:48
Show Gist options
  • Save johnfmorton/636673 to your computer and use it in GitHub Desktop.
Save johnfmorton/636673 to your computer and use it in GitHub Desktop.
Is it a credit card function for JS class
Number.prototype.isCreditCard = (
function() {
var testCC = this.toString();
var sum =0; mul = 1; l = testCC.length;
if (l < 19) {
for (i = 0; i < l; i++)
{
var digit = testCC.substring(l-i-1,l-i);
var tproduct = parseInt(digit ,10)*mul;
if (tproduct >= 10)
sum += (tproduct % 10) + 1;
else
sum += tproduct;
if (mul == 1)
mul++;
else
mul--;
}
if ((sum % 10) == 0) {
return true;
}
}
return false;
}
);
(5555555555554444).isCreditCard();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment