Created
March 8, 2012 19:18
-
-
Save mstudio/2002826 to your computer and use it in GitHub Desktop.
Exercise 4
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); | |
var sum = 0; var mul = 1; var l = CC.length; var digit; var tproduct; | |
for (var i = 0; i < l; i++) { | |
digit = CC.substring(l - i - 1, l - i); | |
tproduct = parseInt(digit, 10) * mul; | |
sum = (tproduct >= 10) ? (sum + (tproduct % 10) + 1) : (sum + tproduct); | |
mul = (mul == 1) ? mul+1 : mul-1; | |
} | |
return ((sum % 10) == 0); | |
} | |
console.log(isCreditCard('4111111111111111')); | |
console.log(isCreditCard('378282246310005')); | |
console.log(isCreditCard('5555555555554444')); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment