Skip to content

Commit 64317c9

Browse files
HD-40307gHD-40307g
HD-40307g
authored and
HD-40307g
committed
reformat LuhnAlgorithm.js and LuhnAlgorithm.test.js using Prettier
1 parent 4141a38 commit 64317c9

File tree

2 files changed

+153
-117
lines changed

2 files changed

+153
-117
lines changed

Diff for: Maths/LuhnAlgorithm.js

+77-59
Original file line numberDiff line numberDiff line change
@@ -1,96 +1,114 @@
11
/**
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.
33
* 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.
55
* 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.
77
* It is not intended to be a cryptographically secure hash function but rather a simple method to detect accidental errors, not malicious attacks.
88
* https://en.wikipedia.org/wiki/Luhn_algorithm
99
*/
1010

1111
// 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]
1717

1818
// 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]
2424

2525
// 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]
3133

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+
]
3351

3452
/**
3553
* Validates a credit card number using the Luhn Algorithm.
3654
* @param {number[]} numArr - Array representing the credit card number.
3755
* @returns {boolean} - True if the card is valid, false otherwise.
3856
*/
3957
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+
}
4967
}
50-
total += currValue;
51-
}
52-
return total % 10 === 0;
53-
};
68+
total += currValue
69+
}
70+
return total % 10 === 0
71+
}
5472

5573
/**
5674
* Finds invalid credit card numbers from a batch.
5775
* @param {number[][]} cards - Array of credit card numbers.
5876
* @returns {number[][]} - Array of invalid credit card numbers.
5977
*/
6078
export function findInvalidCards(cards) {
61-
return cards.filter(card => !validateCred(card));
62-
};
79+
return cards.filter((card) => !validateCred(card))
80+
}
6381

6482
/**
6583
* Identifies credit card companies that issued invalid card numbers.
6684
* @param {number[][]} invalidBatch - Array of invalid credit card numbers.
6785
* @returns {string[]} - Array of company names.
6886
*/
6987
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+
}
91109

92110
// 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

Diff for: Maths/test/LuhnAlgorithm.test.js

+76-58
Original file line numberDiff line numberDiff line change
@@ -1,62 +1,80 @@
11
// Import the functions to be tested
2-
import { validateCred, findInvalidCards, idInvalidCardCompanies } from '../LuhnAlgorithm.js';
2+
import {
3+
validateCred,
4+
findInvalidCards,
5+
idInvalidCardCompanies
6+
} from '../LuhnAlgorithm.js'
37
// Import credit card numbers to be tested
4-
import { valid1, valid2, valid3, valid4, valid5, invalid1, invalid2, invalid3, invalid4, invalid5, batch } from '../LuhnAlgorithm.js';
8+
import {
9+
valid1,
10+
valid2,
11+
valid3,
12+
valid4,
13+
valid5,
14+
invalid1,
15+
invalid2,
16+
invalid3,
17+
invalid4,
18+
invalid5,
19+
batch
20+
} from '../LuhnAlgorithm.js'
521

622
describe('Luhn Algorithm Tests', () => {
7-
// Test for validateCred function
8-
describe('validateCred', () => {
9-
test('should return true for valid credit cards', () => {
10-
const validCards = [valid1, valid2, valid3, valid4, valid5];
11-
validCards.forEach(card => {
12-
expect(validateCred(card)).toBe(true);
13-
});
14-
});
15-
16-
test('should return false for invalid credit cards', () => {
17-
const invalidCards = [invalid1, invalid2, invalid3, invalid4, invalid5];
18-
invalidCards.forEach(card => {
19-
expect(validateCred(card)).toBe(false);
20-
});
21-
});
22-
});
23-
24-
// Test for findInvalidCards function
25-
describe('findInvalidCards', () => {
26-
test('should return only invalid credit cards from a batch', () => {
27-
const invalidCards = findInvalidCards(batch);
28-
const allInvalid = invalidCards.every(card => !validateCred(card));
29-
expect(allInvalid).toBe(true);
30-
});
31-
32-
test('should return an empty array if no invalid cards are found', () => {
33-
const validCards = [valid1, valid2, valid3, valid4, valid5];
34-
const invalidCards = findInvalidCards(validCards);
35-
expect(invalidCards).toEqual([]);
36-
});
37-
});
38-
39-
// Test for idInvalidCardCompanies function
40-
describe('idInvalidCardCompanies', () => {
41-
test('should identify all companies that issued invalid cards', () => {
42-
const invalidCards = findInvalidCards(batch);
43-
const companies = idInvalidCardCompanies(invalidCards);
44-
45-
// Expect the companies array to include known companies
46-
expect(companies).toEqual(expect.arrayContaining(['Visa', 'Mastercard', 'Amex', 'Discover']));
47-
});
48-
49-
test('should return an empty array if no invalid cards are found', () => {
50-
const validCards = [valid1, valid2, valid3, valid4, valid5];
51-
const invalidCards = findInvalidCards(validCards); // Ensure only invalid cards are processed
52-
const companies = idInvalidCardCompanies(invalidCards);
53-
expect(companies).toEqual([]);
54-
});
55-
56-
test('should handle unknown card companies gracefully', () => {
57-
const unknownCard = [9, 3, 1, 8, 2, 0, 7, 4, 5, 6, 7, 8, 9];
58-
const companies = idInvalidCardCompanies([unknownCard]);
59-
expect(companies).not.toContain('Unknown');
60-
});
61-
});
62-
});
23+
// Test for validateCred function
24+
describe('validateCred', () => {
25+
test('should return true for valid credit cards', () => {
26+
const validCards = [valid1, valid2, valid3, valid4, valid5]
27+
validCards.forEach((card) => {
28+
expect(validateCred(card)).toBe(true)
29+
})
30+
})
31+
32+
test('should return false for invalid credit cards', () => {
33+
const invalidCards = [invalid1, invalid2, invalid3, invalid4, invalid5]
34+
invalidCards.forEach((card) => {
35+
expect(validateCred(card)).toBe(false)
36+
})
37+
})
38+
})
39+
40+
// Test for findInvalidCards function
41+
describe('findInvalidCards', () => {
42+
test('should return only invalid credit cards from a batch', () => {
43+
const invalidCards = findInvalidCards(batch)
44+
const allInvalid = invalidCards.every((card) => !validateCred(card))
45+
expect(allInvalid).toBe(true)
46+
})
47+
48+
test('should return an empty array if no invalid cards are found', () => {
49+
const validCards = [valid1, valid2, valid3, valid4, valid5]
50+
const invalidCards = findInvalidCards(validCards)
51+
expect(invalidCards).toEqual([])
52+
})
53+
})
54+
55+
// Test for idInvalidCardCompanies function
56+
describe('idInvalidCardCompanies', () => {
57+
test('should identify all companies that issued invalid cards', () => {
58+
const invalidCards = findInvalidCards(batch)
59+
const companies = idInvalidCardCompanies(invalidCards)
60+
61+
// Expect the companies array to include known companies
62+
expect(companies).toEqual(
63+
expect.arrayContaining(['Visa', 'Mastercard', 'Amex', 'Discover'])
64+
)
65+
})
66+
67+
test('should return an empty array if no invalid cards are found', () => {
68+
const validCards = [valid1, valid2, valid3, valid4, valid5]
69+
const invalidCards = findInvalidCards(validCards) // Ensure only invalid cards are processed
70+
const companies = idInvalidCardCompanies(invalidCards)
71+
expect(companies).toEqual([])
72+
})
73+
74+
test('should handle unknown card companies gracefully', () => {
75+
const unknownCard = [9, 3, 1, 8, 2, 0, 7, 4, 5, 6, 7, 8, 9]
76+
const companies = idInvalidCardCompanies([unknownCard])
77+
expect(companies).not.toContain('Unknown')
78+
})
79+
})
80+
})

0 commit comments

Comments
 (0)