|
1 | 1 | /**
|
2 |
| - * Luhn algorithm, also known as the “Modulus 10” or “mod 10” algorithm. |
| 2 | + * Luhn algorithm, also known as the “Modulus 10” or “mod 10” algorithm. |
3 | 3 | * This algorithm is a simple checksum formula used to validate the authenticity of credit card numbers.
|
4 |
| - * The Luhn algorithm works by doubling every second digit from right to left, adding the resulting digits, and then checking if the total is divisible by 10. |
| 4 | + * The Luhn algorithm works by doubling every second digit from right to left, adding the resulting digits, and then checking if the total is divisible by 10. |
5 | 5 | * If it is, the credit card number is considered valid.
|
6 |
| - * The Luhn algorithm is widely used in credit card verification and is specified in ISO/IEC 7812-1. |
| 6 | + * The Luhn algorithm is widely used in credit card verification and is specified in ISO/IEC 7812-1. |
7 | 7 | * It is not intended to be a cryptographically secure hash function but rather a simple method to detect accidental errors, not malicious attacks.
|
8 | 8 | * https://en.wikipedia.org/wiki/Luhn_algorithm
|
9 | 9 | */
|
10 | 10 |
|
11 | 11 | // Valid credit card numbers
|
12 |
| -export const valid1 = [4, 5, 3, 9, 6, 7, 7, 9, 0, 8, 0, 1, 6, 8, 0, 8]; |
13 |
| -export const valid2 = [5, 5, 3, 5, 7, 6, 6, 7, 6, 8, 7, 5, 1, 4, 3, 9]; |
14 |
| -export const valid3 = [6, 0, 1, 1, 1, 4, 4, 3, 4, 0, 6, 8, 2, 9, 0, 5]; |
15 |
| -export const valid4 = [3, 7, 1, 6, 1, 2, 0, 1, 9, 9, 8, 5, 2, 3, 6]; |
16 |
| -export const valid5 = [4, 5, 3, 9, 4, 0, 4, 9, 6, 7, 8, 6, 9, 6, 6, 6]; |
| 12 | +export const valid1 = [4, 5, 3, 9, 6, 7, 7, 9, 0, 8, 0, 1, 6, 8, 0, 8] |
| 13 | +export const valid2 = [5, 5, 3, 5, 7, 6, 6, 7, 6, 8, 7, 5, 1, 4, 3, 9] |
| 14 | +export const valid3 = [6, 0, 1, 1, 1, 4, 4, 3, 4, 0, 6, 8, 2, 9, 0, 5] |
| 15 | +export const valid4 = [3, 7, 1, 6, 1, 2, 0, 1, 9, 9, 8, 5, 2, 3, 6] |
| 16 | +export const valid5 = [4, 5, 3, 9, 4, 0, 4, 9, 6, 7, 8, 6, 9, 6, 6, 6] |
17 | 17 |
|
18 | 18 | // Invalid credit card numbers
|
19 |
| -export const invalid1 = [4, 5, 3, 2, 7, 7, 8, 7, 7, 1, 0, 9, 1, 7, 9, 5]; |
20 |
| -export const invalid2 = [5, 7, 9, 5, 5, 9, 3, 3, 9, 2, 1, 3, 4, 6, 4, 3]; |
21 |
| -export const invalid3 = [3, 7, 5, 7, 9, 6, 0, 8, 4, 4, 5, 9, 9, 1, 4]; |
22 |
| -export const invalid4 = [6, 0, 1, 1, 1, 2, 7, 9, 6, 1, 7, 7, 7, 9, 3, 5]; |
23 |
| -export const invalid5 = [5, 3, 8, 2, 0, 1, 9, 7, 7, 2, 8, 8, 3, 8, 5, 4]; |
| 19 | +export const invalid1 = [4, 5, 3, 2, 7, 7, 8, 7, 7, 1, 0, 9, 1, 7, 9, 5] |
| 20 | +export const invalid2 = [5, 7, 9, 5, 5, 9, 3, 3, 9, 2, 1, 3, 4, 6, 4, 3] |
| 21 | +export const invalid3 = [3, 7, 5, 7, 9, 6, 0, 8, 4, 4, 5, 9, 9, 1, 4] |
| 22 | +export const invalid4 = [6, 0, 1, 1, 1, 2, 7, 9, 6, 1, 7, 7, 7, 9, 3, 5] |
| 23 | +export const invalid5 = [5, 3, 8, 2, 0, 1, 9, 7, 7, 2, 8, 8, 3, 8, 5, 4] |
24 | 24 |
|
25 | 25 | // Mixed batch of credit card numbers
|
26 |
| -export const mystery1 = [3, 4, 4, 8, 0, 1, 9, 6, 8, 3, 0, 5, 4, 1, 4]; |
27 |
| -export const mystery2 = [5, 4, 6, 6, 1, 0, 0, 8, 6, 1, 6, 2, 0, 2, 3, 9]; |
28 |
| -export const mystery3 = [6, 0, 1, 1, 3, 7, 7, 0, 2, 0, 9, 6, 2, 6, 5, 6, 2, 0, 3]; |
29 |
| -export const mystery4 = [4, 9, 2, 9, 8, 7, 7, 1, 6, 9, 2, 1, 7, 0, 9, 3]; |
30 |
| -export const mystery5 = [4, 9, 1, 3, 5, 4, 0, 4, 6, 3, 0, 7, 2, 5, 2, 3]; |
| 26 | +export const mystery1 = [3, 4, 4, 8, 0, 1, 9, 6, 8, 3, 0, 5, 4, 1, 4] |
| 27 | +export const mystery2 = [5, 4, 6, 6, 1, 0, 0, 8, 6, 1, 6, 2, 0, 2, 3, 9] |
| 28 | +export const mystery3 = [ |
| 29 | + 6, 0, 1, 1, 3, 7, 7, 0, 2, 0, 9, 6, 2, 6, 5, 6, 2, 0, 3 |
| 30 | +] |
| 31 | +export const mystery4 = [4, 9, 2, 9, 8, 7, 7, 1, 6, 9, 2, 1, 7, 0, 9, 3] |
| 32 | +export const mystery5 = [4, 9, 1, 3, 5, 4, 0, 4, 6, 3, 0, 7, 2, 5, 2, 3] |
31 | 33 |
|
32 |
| -export const batch = [valid1, valid2, valid3, valid4, valid5, invalid1, invalid2, invalid3, invalid4, invalid5, mystery1, mystery2, mystery3, mystery4, mystery5]; |
| 34 | +export const batch = [ |
| 35 | + valid1, |
| 36 | + valid2, |
| 37 | + valid3, |
| 38 | + valid4, |
| 39 | + valid5, |
| 40 | + invalid1, |
| 41 | + invalid2, |
| 42 | + invalid3, |
| 43 | + invalid4, |
| 44 | + invalid5, |
| 45 | + mystery1, |
| 46 | + mystery2, |
| 47 | + mystery3, |
| 48 | + mystery4, |
| 49 | + mystery5 |
| 50 | +] |
33 | 51 |
|
34 | 52 | /**
|
35 | 53 | * Validates a credit card number using the Luhn Algorithm.
|
36 | 54 | * @param {number[]} numArr - Array representing the credit card number.
|
37 | 55 | * @returns {boolean} - True if the card is valid, false otherwise.
|
38 | 56 | */
|
39 | 57 | export function validateCred(numArr) {
|
40 |
| - let total = 0; |
41 |
| - for (let i = numArr.length - 1; i >= 0; i--) { |
42 |
| - let currValue = numArr[i]; |
43 |
| - // Double every other digit starting from the second-to-last |
44 |
| - if ((numArr.length - 1 - i) % 2 === 1) { |
45 |
| - currValue *= 2; |
46 |
| - if (currValue > 9) { |
47 |
| - currValue -= 9; |
48 |
| - } |
| 58 | + let total = 0 |
| 59 | + for (let i = numArr.length - 1; i >= 0; i--) { |
| 60 | + let currValue = numArr[i] |
| 61 | + // Double every other digit starting from the second-to-last |
| 62 | + if ((numArr.length - 1 - i) % 2 === 1) { |
| 63 | + currValue *= 2 |
| 64 | + if (currValue > 9) { |
| 65 | + currValue -= 9 |
| 66 | + } |
49 | 67 | }
|
50 |
| - total += currValue; |
51 |
| - } |
52 |
| - return total % 10 === 0; |
53 |
| -}; |
| 68 | + total += currValue |
| 69 | + } |
| 70 | + return total % 10 === 0 |
| 71 | +} |
54 | 72 |
|
55 | 73 | /**
|
56 | 74 | * Finds invalid credit card numbers from a batch.
|
57 | 75 | * @param {number[][]} cards - Array of credit card numbers.
|
58 | 76 | * @returns {number[][]} - Array of invalid credit card numbers.
|
59 | 77 | */
|
60 | 78 | export function findInvalidCards(cards) {
|
61 |
| - return cards.filter(card => !validateCred(card)); |
62 |
| -}; |
| 79 | + return cards.filter((card) => !validateCred(card)) |
| 80 | +} |
63 | 81 |
|
64 | 82 | /**
|
65 | 83 | * Identifies credit card companies that issued invalid card numbers.
|
66 | 84 | * @param {number[][]} invalidBatch - Array of invalid credit card numbers.
|
67 | 85 | * @returns {string[]} - Array of company names.
|
68 | 86 | */
|
69 | 87 | export function idInvalidCardCompanies(invalidBatch) {
|
70 |
| - const companies = new Set(); |
71 |
| - invalidBatch.forEach(card => { |
72 |
| - switch (card[0]) { |
73 |
| - case 3: |
74 |
| - companies.add('Amex'); |
75 |
| - break; |
76 |
| - case 4: |
77 |
| - companies.add('Visa'); |
78 |
| - break; |
79 |
| - case 5: |
80 |
| - companies.add('Mastercard'); |
81 |
| - break; |
82 |
| - case 6: |
83 |
| - companies.add('Discover'); |
84 |
| - break; |
85 |
| - default: |
86 |
| - console.warn('Company not found for card starting with:', card[0]); |
87 |
| - } |
88 |
| - }); |
89 |
| - return Array.from(companies); |
90 |
| -}; |
| 88 | + const companies = new Set() |
| 89 | + invalidBatch.forEach((card) => { |
| 90 | + switch (card[0]) { |
| 91 | + case 3: |
| 92 | + companies.add('Amex') |
| 93 | + break |
| 94 | + case 4: |
| 95 | + companies.add('Visa') |
| 96 | + break |
| 97 | + case 5: |
| 98 | + companies.add('Mastercard') |
| 99 | + break |
| 100 | + case 6: |
| 101 | + companies.add('Discover') |
| 102 | + break |
| 103 | + default: |
| 104 | + console.warn('Company not found for card starting with:', card[0]) |
| 105 | + } |
| 106 | + }) |
| 107 | + return Array.from(companies) |
| 108 | +} |
91 | 109 |
|
92 | 110 | // Example Usage
|
93 |
| -console.log(validateCred(valid1)); // Should print true |
94 |
| -console.log(validateCred(invalid1)); // Should print false |
95 |
| -console.log(findInvalidCards(batch)); // Find all invalid cards |
96 |
| -console.log(idInvalidCardCompanies(findInvalidCards(batch))); // Find companies of invalid cards |
| 111 | +console.log(validateCred(valid1)) // Should print true |
| 112 | +console.log(validateCred(invalid1)) // Should print false |
| 113 | +console.log(findInvalidCards(batch)) // Find all invalid cards |
| 114 | +console.log(idInvalidCardCompanies(findInvalidCards(batch))) // Find companies of invalid cards |
0 commit comments