From 80311093d4c158cbc7332a0e232b972989b9de77 Mon Sep 17 00:00:00 2001 From: Mohsen Zamani Date: Wed, 28 Jan 2026 13:48:24 +0000 Subject: [PATCH 1/4] Complete sprint-3-stretch --- Sprint-3/4-stretch/card-validator.js | 30 ++++++++ Sprint-3/4-stretch/password-validator.js | 12 +++- Sprint-3/4-stretch/password-validator.test.js | 69 ++++++++++++++++--- 3 files changed, 100 insertions(+), 11 deletions(-) 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 000000000..27e6a8db5 --- /dev/null +++ b/Sprint-3/4-stretch/card-validator.js @@ -0,0 +1,30 @@ +function creditCardValidator(cardNum) { + // Splits the cardNum into an array of characters + const cardNumArray = cardNum.split(""); + + // Checks if length is 16, otherwise return false + if (cardNumArray.length !== 16) return false; + + // checks if all digits are numbers + if ( + !cardNumArray.every( + (num) => num.charCodeAt(0) >= 48 && num.charCodeAt(0) <= 57 + ) + ) + return false; + + // Checks if there are at least two different digits + const count = cardNumArray.reduce((acc, curr) => { + acc[curr] = acc[curr] ? acc[curr] + 1 : 1; + return acc; + }, {}); + if (Object.keys(count).length < 2) return false; + + // Checks if the sum of all digits is greater than 16 + const sumOfDigits = cardNumArray.reduce((acc, curr) => acc + Number(curr), 0); + if (sumOfDigits <= 16) return false; + + //checks if last digit is even + if (cardNumArray[cardNumArray.length - 1] % 2 !== 0) return false; + return true; +} diff --git a/Sprint-3/4-stretch/password-validator.js b/Sprint-3/4-stretch/password-validator.js index b55d527db..c5862420c 100644 --- a/Sprint-3/4-stretch/password-validator.js +++ b/Sprint-3/4-stretch/password-validator.js @@ -1,6 +1,12 @@ +const previousPasswords = ["5B43n21"]; function passwordValidator(password) { - return password.length < 5 ? false : true -} + if (password.length < 5) return false; + if (previousPasswords.includes(password)) return false; + previousPasswords.push(password); + + const rules = [/[A-Z]/, /[a-z]/, /[0-9]/, /[!#$%.*&]/]; + return rules.every((rule) => rule.test(password)); +} -module.exports = passwordValidator; \ No newline at end of file +module.exports = passwordValidator; diff --git a/Sprint-3/4-stretch/password-validator.test.js b/Sprint-3/4-stretch/password-validator.test.js index 8fa3089d6..7fc179591 100644 --- a/Sprint-3/4-stretch/password-validator.test.js +++ b/Sprint-3/4-stretch/password-validator.test.js @@ -16,11 +16,64 @@ You must breakdown this problem in order to solve it. Find one test case first a */ 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 + // Arrange + const password = "A1b2"; + // Act + const result = isValidPassword(password); + // Assert + expect(result).toEqual(false); +}); + +test("password is not previously used", () => { + // Arrange + const password = "5B43n21"; + // Act + const result = isValidPassword(password); + // Assert + expect(result).toEqual(false); +}); + +test("password contains at least one uppercase English letter", () => { + // Arrange + const password = "1a2345"; + // Act + const result = isValidPassword(password); + // Assert + expect(result).toEqual(false); +}); + +test("password contains at least one uppercase English letter", () => { + // Arrange + const password = "1B2345"; + // Act + const result = isValidPassword(password); + // Assert + expect(result).toEqual(false); +}); + +test("password contains at least one number(0-9)", () => { + // Arrange + const password = "sdkerjJNGk"; + // Act + const result = isValidPassword(password); + // Assert + expect(result).toEqual(false); +}); + +test('password contains at least one of "!", "#", "$", "%", ".", "*", "&"', () => { + // Arrange + const password = "sdkerjJNG23k"; + // Act + const result = isValidPassword(password); + // Assert + expect(result).toEqual(false); +}); + +test("password meets all the condition for a valid password and passwordValidator returns true", () => { + // Arrange + const password = "sdkerjJNG23k&"; + // Act + const result = isValidPassword(password); + // Assert + expect(result).toEqual(true); +}); From c7533d7e3a3840bb02f2879a452cc99c78ced43a Mon Sep 17 00:00:00 2001 From: Mohsen Zamani Date: Tue, 17 Feb 2026 09:49:13 +0000 Subject: [PATCH 2/4] Apply changes commented by the reviewer --- Sprint-3/4-stretch/card-validator.js | 15 +++------- Sprint-3/4-stretch/password-validator.js | 3 +- Sprint-3/4-stretch/password-validator.test.js | 30 +++++++++---------- 3 files changed, 20 insertions(+), 28 deletions(-) diff --git a/Sprint-3/4-stretch/card-validator.js b/Sprint-3/4-stretch/card-validator.js index 27e6a8db5..330e0223b 100644 --- a/Sprint-3/4-stretch/card-validator.js +++ b/Sprint-3/4-stretch/card-validator.js @@ -6,19 +6,11 @@ function creditCardValidator(cardNum) { if (cardNumArray.length !== 16) return false; // checks if all digits are numbers - if ( - !cardNumArray.every( - (num) => num.charCodeAt(0) >= 48 && num.charCodeAt(0) <= 57 - ) - ) - return false; + if (!cardNumArray.every((num) => num >= 0 && num <= 9)) return false; // Checks if there are at least two different digits - const count = cardNumArray.reduce((acc, curr) => { - acc[curr] = acc[curr] ? acc[curr] + 1 : 1; - return acc; - }, {}); - if (Object.keys(count).length < 2) return false; + const count = new Set(cardNumArray); + if (count.size < 2) return false; // Checks if the sum of all digits is greater than 16 const sumOfDigits = cardNumArray.reduce((acc, curr) => acc + Number(curr), 0); @@ -28,3 +20,4 @@ function creditCardValidator(cardNum) { if (cardNumArray[cardNumArray.length - 1] % 2 !== 0) return false; return true; } +console.log(creditCardValidator("1111111111111111")); diff --git a/Sprint-3/4-stretch/password-validator.js b/Sprint-3/4-stretch/password-validator.js index c5862420c..068423344 100644 --- a/Sprint-3/4-stretch/password-validator.js +++ b/Sprint-3/4-stretch/password-validator.js @@ -1,5 +1,4 @@ -const previousPasswords = ["5B43n21"]; -function passwordValidator(password) { +function passwordValidator(password, previousPasswords = []) { if (password.length < 5) return false; if (previousPasswords.includes(password)) return false; previousPasswords.push(password); diff --git a/Sprint-3/4-stretch/password-validator.test.js b/Sprint-3/4-stretch/password-validator.test.js index 7fc179591..c1f52aa7d 100644 --- a/Sprint-3/4-stretch/password-validator.test.js +++ b/Sprint-3/4-stretch/password-validator.test.js @@ -15,52 +15,52 @@ To be valid, a password must: 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", () => { +test("should return false if password has fewer than 5 characters", () => { // Arrange - const password = "A1b2"; + const password = "A1b&"; // Act const result = isValidPassword(password); // Assert expect(result).toEqual(false); }); -test("password is not previously used", () => { +test("should return false if password was previously used", () => { // Arrange - const password = "5B43n21"; + const password = "5B43n21!"; // Act - const result = isValidPassword(password); + const result = isValidPassword(password, "5B43n21!"); // Assert expect(result).toEqual(false); }); -test("password contains at least one uppercase English letter", () => { +test("should return false if password does not contain an uppercase English letter", () => { // Arrange - const password = "1a2345"; + const password = "1a2345&"; // Act const result = isValidPassword(password); // Assert expect(result).toEqual(false); }); -test("password contains at least one uppercase English letter", () => { +test("should return false if password does not contain an lowercase English letter", () => { // Arrange - const password = "1B2345"; + const password = "1B2345%"; // Act const result = isValidPassword(password); // Assert expect(result).toEqual(false); }); -test("password contains at least one number(0-9)", () => { +test("should return false if password has no numbers(0-9)", () => { // Arrange - const password = "sdkerjJNGk"; + const password = "se!rjJN%Gk"; // Act const result = isValidPassword(password); // Assert expect(result).toEqual(false); }); -test('password contains at least one of "!", "#", "$", "%", ".", "*", "&"', () => { +test('should return false if password has no special characters including "!", "#", "$", "%", ".", "*", "&"', () => { // Arrange const password = "sdkerjJNG23k"; // Act @@ -69,11 +69,11 @@ test('password contains at least one of "!", "#", "$", "%", ".", "*", "&"', () = expect(result).toEqual(false); }); -test("password meets all the condition for a valid password and passwordValidator returns true", () => { +test("should return true if password meets all validation rules", () => { // Arrange - const password = "sdkerjJNG23k&"; + const password = "sdkerj!JNG23k&"; // Act - const result = isValidPassword(password); + const result = isValidPassword(password, ["se!rjJN%G6k", "1B2h345%a"]); // Assert expect(result).toEqual(true); }); From f2e057f9734c61f9dbaf04ce9c22cdacf6b279cd Mon Sep 17 00:00:00 2001 From: Mohsen Zamani Date: Tue, 17 Feb 2026 15:47:01 +0000 Subject: [PATCH 3/4] Apply changes commented by reviewer --- Sprint-3/4-stretch/card-validator.js | 2 +- Sprint-3/4-stretch/password-validator.test.js | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/Sprint-3/4-stretch/card-validator.js b/Sprint-3/4-stretch/card-validator.js index 330e0223b..6fe2084bb 100644 --- a/Sprint-3/4-stretch/card-validator.js +++ b/Sprint-3/4-stretch/card-validator.js @@ -6,7 +6,7 @@ function creditCardValidator(cardNum) { if (cardNumArray.length !== 16) return false; // checks if all digits are numbers - if (!cardNumArray.every((num) => num >= 0 && num <= 9)) return false; + if (!cardNumArray.every((num) => num >= "0" && num <= "9")) return false; // Checks if there are at least two different digits const count = new Set(cardNumArray); diff --git a/Sprint-3/4-stretch/password-validator.test.js b/Sprint-3/4-stretch/password-validator.test.js index c1f52aa7d..62e5ee203 100644 --- a/Sprint-3/4-stretch/password-validator.test.js +++ b/Sprint-3/4-stretch/password-validator.test.js @@ -51,7 +51,7 @@ test("should return false if password does not contain an lowercase English lett expect(result).toEqual(false); }); -test("should return false if password has no numbers(0-9)", () => { +test("should return false if password has no digit", () => { // Arrange const password = "se!rjJN%Gk"; // Act From b18826555872a97a570c3b657bd561a13a0e638d Mon Sep 17 00:00:00 2001 From: Mohsen Zamani Date: Tue, 17 Feb 2026 18:14:22 +0000 Subject: [PATCH 4/4] Apply changes commented by reviewer --- Sprint-3/4-stretch/password-validator.js | 2 ++ Sprint-3/4-stretch/password-validator.test.js | 9 +++++++++ 2 files changed, 11 insertions(+) diff --git a/Sprint-3/4-stretch/password-validator.js b/Sprint-3/4-stretch/password-validator.js index 068423344..7609f92dd 100644 --- a/Sprint-3/4-stretch/password-validator.js +++ b/Sprint-3/4-stretch/password-validator.js @@ -1,4 +1,6 @@ function passwordValidator(password, previousPasswords = []) { + if (!Array.isArray(previousPasswords)) return false; + if (password.length < 5) return false; if (previousPasswords.includes(password)) return false; previousPasswords.push(password); diff --git a/Sprint-3/4-stretch/password-validator.test.js b/Sprint-3/4-stretch/password-validator.test.js index 62e5ee203..bfd9e5299 100644 --- a/Sprint-3/4-stretch/password-validator.test.js +++ b/Sprint-3/4-stretch/password-validator.test.js @@ -77,3 +77,12 @@ test("should return true if password meets all validation rules", () => { // Assert expect(result).toEqual(true); }); + +test("should return false if previousPassword is not an array", () => { + // Arrange + const password = "sdkerj!JNG23k&"; + // Act + const result = isValidPassword(password, "1B2h345%a"); + // Assert + expect(result).toEqual(false); +});