Skip to content

Commit bcecc56

Browse files
committed
Implemented credit card validator according to Sprint 3 stretch requirements
1 parent 37c47d1 commit bcecc56

File tree

1 file changed

+24
-0
lines changed

1 file changed

+24
-0
lines changed
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
function validateCreditCard(number) {
2+
// Ensure number is 16 digits and only contains digits
3+
if (number.length !== 16 || !/^\d+$/.test(number)) return false;
4+
5+
// Ensure at least 2 different digits are present
6+
const uniqueDigits = new Set(number);
7+
if (uniqueDigits.size < 2) return false;
8+
9+
// Ensure the last digit is even
10+
const lastDigit = parseInt(number[number.length - 1], 10);
11+
if (lastDigit % 2 !== 0) return false;
12+
13+
// Ensure the sum of digits is greater than 16
14+
const sum = number.split('').reduce((acc, digit) => acc + parseInt(digit, 10), 0);
15+
if (sum <= 16) return false;
16+
17+
// If all checks pass, the number is valid
18+
return true;
19+
}
20+
// Function to validate a credit card number according to the rules in card-validator.md
21+
22+
console.log(validateCreditCard('9999777788880000')); // true
23+
console.log(validateCreditCard('4444444444444444')); // false
24+
console.log(validateCreditCard('6666666666666661')); // false

0 commit comments

Comments
 (0)