Created
November 12, 2014 20:32
-
-
Save wmantly/7e23738f21cffcb011e6 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
Credit Card Validator | |
Using the Luhn Algorithm, also known as "modulus 10", we will be determining the validity of a given credit card number. | |
For now, we are just editing the included .js file. You will find the skeleton of the CreditCard prototype inside. | |
The Class | |
We want our class to have its three main properties set on instantiation - ccNum, cardType, and valid. If the card number given passes the Luhn algorithm, valid should be true and cardType should be set to 'VISA', 'AMEX', etc. If it does not pass, valid should be false and cardType should be set to "INVALID" | |
This way, we can do this: | |
myCard = new CreditCard('347650202246884') | |
myCard.valid // true | |
myCard.cardType // 'AMEX' | |
myCard.ccNum // '347650202246884' | |
There are three prototype functions. | |
CreditCard.prototype.validStartingNums should check whether or not the credit card has valid starting numbers. | |
Visa must start with 4 | |
Mastercard must start with 51, 52, 53, 54 or 55 | |
AMEX must start with 34 or 37 | |
Discover must start with 6011 | |
CreditCard.prototype.checkLength should check whether or not a credit card number is a valid length. | |
Visa, MC and Discover have 16 digits | |
AMEX have 15 | |
CreditCard.prototype.validate should run the Luhn Algorithm and return true or false. | |
The Algorithm | |
From the right most digit, double the value of every second digit. For example: | |
4 4 8 5 0 4 0 9 9 3 2 8 7 6 1 6 | |
becomes | |
8 4 16 5 0 4 0 9 18 3 4 8 14 6 2 6 | |
Now, sum all the individual digits together. That is to say, split any numbers with two digits into their individual digits and add them. Like so: | |
8 + 4 + 1 + 6 + 5 + 0 + 4 + 0 + 9 + 1 + 8 + 3 + 4 + 8 + 1 + 4 + 6 + 2 + 6 | |
Now take the sum of those numbers and modulo 10. | |
80 % 10 | |
If the result is 0, the credit card number is valid. | |
Tests | |
Make sure your code passes all the assert tests. | |
Write your own assert tests to test any other possible cases where your code might fail. | |
Goals | |
Keep your code super clean and DRY. | |
If you are repeating yourself, stop and think about how to better approach the problem. | |
Keep your code encapsulated - imagine your CreditCard class is an interface other code will need to read. You want it as separate and unentangled as possible. Your class should not be dependent on any code outside of it - except, of course, code to instantiate it.Credit Card Validator | |
Using the Luhn Algorithm, also known as "modulus 10", we will be determining the validity of a given credit card number. | |
For now, we are just editing the included .js file. You will find the skeleton of the CreditCard prototype inside. | |
The Class | |
We want our class to have its three main properties set on instantiation - ccNum, cardType, and valid. If the card number given passes the Luhn algorithm, valid should be true and cardType should be set to 'VISA', 'AMEX', etc. If it does not pass, valid should be false and cardType should be set to "INVALID" | |
This way, we can do this: | |
myCard = new CreditCard('347650202246884') | |
myCard.valid // true | |
myCard.cardType // 'AMEX' | |
myCard.ccNum // '347650202246884' | |
There are three prototype functions. | |
CreditCard.prototype.validStartingNums should check whether or not the credit card has valid starting numbers. | |
Visa must start with 4 | |
Mastercard must start with 51, 52, 53, 54 or 55 | |
AMEX must start with 34 or 37 | |
Discover must start with 6011 | |
CreditCard.prototype.checkLength should check whether or not a credit card number is a valid length. | |
Visa, MC and Discover have 16 digits | |
AMEX have 15 | |
CreditCard.prototype.validate should run the Luhn Algorithm and return true or false. | |
The Algorithm | |
From the right most digit, double the value of every second digit. For example: | |
4 4 8 5 0 4 0 9 9 3 2 8 7 6 1 6 | |
becomes | |
8 4 16 5 0 4 0 9 18 3 4 8 14 6 2 6 | |
Now, sum all the individual digits together. That is to say, split any numbers with two digits into their individual digits and add them. Like so: | |
8 + 4 + 1 + 6 + 5 + 0 + 4 + 0 + 9 + 1 + 8 + 3 + 4 + 8 + 1 + 4 + 6 + 2 + 6 | |
Now take the sum of those numbers and modulo 10. | |
80 % 10 | |
If the result is 0, the credit card number is valid. | |
Tests | |
Make sure your code passes all the assert tests. | |
Write your own assert tests to test any other possible cases where your code might fail. | |
Goals | |
Keep your code super clean and DRY. | |
If you are repeating yourself, stop and think about how to better approach the problem. | |
Keep your code encapsulated - imagine your CreditCard class is an interface other code will need to read. You want it as separate and unentangled as possible. Your class should not be dependent on any code outside of it - except, of course, code to instantiate it. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment