Skip to content

Instantly share code, notes, and snippets.

@jasonmw
Created November 10, 2011 19:44
Show Gist options
  • Save jasonmw/1355942 to your computer and use it in GitHub Desktop.
Save jasonmw/1355942 to your computer and use it in GitHub Desktop.
function isCreditCard( CC ) {
if (CC.length > 19)
return (false);
sum = 0; multiplier = 1; l = CC.length;
for (i = 0; i < l; i++)
{
digit_to_process = getCharAtIndexFromEndOfString(CC,i);
tmp_product = parseInt(digit_to_process) * multiplier;
sum += getProduct(tmp_product);
multiplier = flip(multiplier);
}
return sum % 10 == 0;
}
function getProduct(number){
return number >=10 ? (tproduct % 10) + 1 : number;
}
function flip(num){
return num == 1 ? 2 : 1;
}
function getCharAtIndexFromEndOfString(ccstring,index,length){
ccstring.substring(ccstring.length-index-1,ccstring.length-index);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment