Skip to content

Instantly share code, notes, and snippets.

@jamesmenera
Created March 7, 2013 18:37
Show Gist options
  • Save jamesmenera/5110548 to your computer and use it in GitHub Desktop.
Save jamesmenera/5110548 to your computer and use it in GitHub Desktop.
example4
// 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
var isCreditCard = function(CC) {
if (CC.length > 19) {
return (false);
}
var sum = 0,
mul = 1,
length = CC.length,
digit,
tproduct;
for (i = 0; i < l; i++)
{
digit = CC.substring(l-i-1,l-i);
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);
else
return (false);
}
document.body.innerHTML = new isCreditCard('5555555555554444');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment