Created
November 10, 2011 19:44
-
-
Save jasonmw/1355942 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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