Created
September 8, 2011 17:53
-
-
Save nickcooley/1204091 to your computer and use it in GitHub Desktop.
This file contains 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
// 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 | |
//This solution is called "Cray-z with Terniaries!!!!1" | |
function isCreditCard( CC ) { | |
if (CC.length > 19) | |
return (false); | |
var sum = 0, | |
mul = 1, | |
len = CC.length; //Variables were going to window.scope. Now local to function | |
for (i = 0; i < len; i++) // is there value in abstracting "for" logic here? | |
{ | |
var digit = CC.substring(len-i-1,len-i), | |
tproduct = parseInt(digit ,10)*mul; //Same variable combination with var declaration. Now I'm wondering if this is stylistic... | |
sum +=(tproduct >= 10)?(tproduct % 10) + 1:(sum += tproduct); //hopefully improved readability by reducing to terniary operator | |
mul+=(mul==1)?1:-1; | |
} | |
return ((sum % 10) == 0)?true:false; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment