Luhn Algorithm (https://en.wikipedia.org/wiki/Luhn_algorithm) is a very popular algorithm among developers. It is mostly used for Credit Card number validations.
The basic idea is you have a set of numbers with length 15 and using these 15 digits you are calculating a check digit for the 16th position.
The Simplified algorithm definition as follows
- Lets take a sample 15 digit number (123456789123456) and you need to get the check digit and complete the sequence.
- First you need to reverse the number sequence – 654321987654321
- You have to take the odd position numbers and need to multiply them by 2 – 12, 8, 4,18,14,10,6,2
- If the multiplied value is grater than 9, we need to subtract 9 from them. – 3,8,4,9,5,1,6,2
- The Even number list is not subjected to change – 5,3,1,8,6,4,2
- We need to add those numbers together and need to get a total
(3+8+4+9+5+1+6+2)+(5+3+1+8+6+4+2) = 67
- To get the check digit we need to subtract the output value from next 10th multiplier. In our case it is 70
70 – 67 = 3
Lets do this in ECMA6Script way…