From c37c56b5e71a9e30e94439fa463c9dfee535176b Mon Sep 17 00:00:00 2001 From: KhotKeys Date: Mon, 27 Jul 2026 22:55:36 +0100 Subject: [PATCH 1/2] Implement passwordValidator with all 6 rules and tests --- Sprint-3/4-stretch/password-validator.js | 13 +++-- Sprint-3/4-stretch/password-validator.test.js | 50 ++++++++++--------- 2 files changed, 37 insertions(+), 26 deletions(-) diff --git a/Sprint-3/4-stretch/password-validator.js b/Sprint-3/4-stretch/password-validator.js index b55d527dba..ff0118d9af 100644 --- a/Sprint-3/4-stretch/password-validator.js +++ b/Sprint-3/4-stretch/password-validator.js @@ -1,6 +1,13 @@ +const previousPasswords = ["Pass1!", "Hello1#", "Secure1$"]; + function passwordValidator(password) { - return password.length < 5 ? false : true + if (password.length < 5) return false; + if (!/[A-Z]/.test(password)) return false; + if (!/[a-z]/.test(password)) return false; + if (!/[0-9]/.test(password)) return false; + if (!/[!#$%.*&]/.test(password)) return false; + if (previousPasswords.includes(password)) return false; + return true; } - -module.exports = passwordValidator; \ No newline at end of file +module.exports = { passwordValidator, previousPasswords }; diff --git a/Sprint-3/4-stretch/password-validator.test.js b/Sprint-3/4-stretch/password-validator.test.js index 8fa3089d6b..f163672f08 100644 --- a/Sprint-3/4-stretch/password-validator.test.js +++ b/Sprint-3/4-stretch/password-validator.test.js @@ -1,26 +1,30 @@ -/* -Password Validation +const { passwordValidator } = require("./password-validator"); -Write a program that should check if a password is valid -and returns a boolean +test("should return false when password has fewer than 5 characters", () => { + expect(passwordValidator("Ab1!")).toEqual(false); +}); -To be valid, a password must: -- Have at least 5 characters. -- Have at least one English uppercase letter (A-Z) -- Have at least one English lowercase letter (a-z) -- Have at least one number (0-9) -- Have at least one of the following non-alphanumeric symbols: ("!", "#", "$", "%", ".", "*", "&") -- Must not be any previous password in the passwords array. +test("should return false when password has no uppercase letter", () => { + expect(passwordValidator("hello1!")).toEqual(false); +}); -You must breakdown this problem in order to solve it. Find one test case first and get that working -*/ -const isValidPassword = require("./password-validator"); -test("password has at least 5 characters", () => { - // Arrange - const password = "12345"; - // Act - const result = isValidPassword(password); - // Assert - expect(result).toEqual(true); -} -); \ No newline at end of file +test("should return false when password has no lowercase letter", () => { + expect(passwordValidator("HELLO1!")).toEqual(false); +}); + +test("should return false when password has no number", () => { + expect(passwordValidator("Hello!")).toEqual(false); +}); + +test("should return false when password has no special character", () => { + expect(passwordValidator("Hello1")).toEqual(false); +}); + +test("should return false when password is a previous password", () => { + expect(passwordValidator("Pass1!")).toEqual(false); +}); + +test("should return true for a valid password meeting all criteria", () => { + expect(passwordValidator("Valid1!")).toEqual(true); + expect(passwordValidator("MyPass1$")).toEqual(true); +}); From f3b13d5a88b676e908af0697a1c655d075cd0f15 Mon Sep 17 00:00:00 2001 From: KhotKeys Date: Mon, 27 Jul 2026 22:55:36 +0100 Subject: [PATCH 2/2] Create card validator with all 4 rules and tests --- Sprint-3/4-stretch/card-validator.js | 26 ++++++++++++++++++++ Sprint-3/4-stretch/card-validator.test.js | 30 +++++++++++++++++++++++ 2 files changed, 56 insertions(+) create mode 100644 Sprint-3/4-stretch/card-validator.js create mode 100644 Sprint-3/4-stretch/card-validator.test.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..a6fb3f2587 --- /dev/null +++ b/Sprint-3/4-stretch/card-validator.js @@ -0,0 +1,26 @@ +// Validates a credit card number string against the following rules: +// - Must be exactly 16 digits (no letters or other characters) +// - Must contain at least two different digits +// - The final digit must be even +// - The sum of all digits must be greater than 16 + +function isValidCardNumber(cardNumber) { + // Rule 1: must be exactly 16 digit characters + if (!/^\d{16}$/.test(cardNumber)) return false; + + const digits = cardNumber.split("").map(Number); + + // Rule 2: at least two different digits must be present + if (new Set(digits).size < 2) return false; + + // Rule 3: final digit must be even + if (digits[15] % 2 !== 0) return false; + + // Rule 4: sum of all digits must be greater than 16 + const sum = digits.reduce((acc, d) => acc + d, 0); + if (sum <= 16) return false; + + return true; +} + +module.exports = isValidCardNumber; diff --git a/Sprint-3/4-stretch/card-validator.test.js b/Sprint-3/4-stretch/card-validator.test.js new file mode 100644 index 0000000000..da5cce03d6 --- /dev/null +++ b/Sprint-3/4-stretch/card-validator.test.js @@ -0,0 +1,30 @@ +const isValidCardNumber = require("./card-validator"); + +test("should return true for a valid card number", () => { + expect(isValidCardNumber("9999777788880000")).toEqual(true); + expect(isValidCardNumber("6666666666661666")).toEqual(true); +}); + +test("should return false when card contains non-digit characters", () => { + expect(isValidCardNumber("a92332119c011112")).toEqual(false); +}); + +test("should return false when card has fewer than 16 digits", () => { + expect(isValidCardNumber("123456789012345")).toEqual(false); +}); + +test("should return false when card has more than 16 digits", () => { + expect(isValidCardNumber("12345678901234567")).toEqual(false); +}); + +test("should return false when all digits are the same", () => { + expect(isValidCardNumber("4444444444444444")).toEqual(false); +}); + +test("should return false when final digit is odd", () => { + expect(isValidCardNumber("6666666666666661")).toEqual(false); +}); + +test("should return false when sum of digits is not greater than 16", () => { + expect(isValidCardNumber("1111111111111110")).toEqual(false); +});