Skip to content

Instantly share code, notes, and snippets.

@nerdpruitt
Last active December 14, 2015 15:49
Show Gist options
  • Save nerdpruitt/5110517 to your computer and use it in GitHub Desktop.
Save nerdpruitt/5110517 to your computer and use it in GitHub Desktop.
// taken from http://www.dreamincode.net/code/snippet154.htm
//
// Find the weaknesses, and clean up the code.
// Find at least 3 things you can make better!
//
// Test numbers (return true):
// 4111111111111111
// 378282246310005
// 5555555555554444
function isCreditCard( CC ) {
var sum = 0, mul = 1, length = CC.length;
if (length > 19)
return (false);
for (i = 0; i < length; i++)
{
var digit = CC.substring(length-i-1, length-i),
tproduct = parseInt(digit ,10)*mul;
sum += ((tproduct >= 10) ? ((tproduct % 10) + 1) : tproduct);
mul == 1 ? mul++ : mul--;
}
return ((sum % 10) == 0);
}
document.body.innerHTML = isCreditCard('4111111111111111')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment