From bbb72fe80383c80a709d8338ec690c98bcf0d7b7 Mon Sep 17 00:00:00 2001 From: Richard Frimpong Date: Fri, 6 Mar 2026 03:27:43 +0000 Subject: [PATCH] Implement credit card validator for Sprint 3 stretch coursework --- Sprint-3/4-stretch/card-validator.js | 14 ++++++++++++++ 1 file changed, 14 insertions(+) create mode 100644 Sprint-3/4-stretch/card-validator.js diff --git a/Sprint-3/4-stretch/card-validator.js b/Sprint-3/4-stretch/card-validator.js new file mode 100644 index 0000000000..95ab482b0a --- /dev/null +++ b/Sprint-3/4-stretch/card-validator.js @@ -0,0 +1,14 @@ +function validateCreditCard(number) { + if (number.length !== 16 || !/^\d+$/.test(number)) return false; + const uniqueDigits = new Set(number); + if (uniqueDigits.size < 2) return false; + const lastDigit = parseInt(number[number.length - 1], 10); + if (lastDigit % 2 !== 0) return false; + const sum = number.split('').reduce((acc, digit) => acc + parseInt(digit, 10), 0); + if (sum <= 16) return false; + return true; +} + +console.log(validateCreditCard('9999777788880000')); // true +console.log(validateCreditCard('4444444444444444')); // false +console.log(validateCreditCard('6666666666666661')); // false \ No newline at end of file