From 4b39af73921e2e6fb9e11a7e4b96a275072ce181 Mon Sep 17 00:00:00 2001 From: UbunMen Date: Fri, 7 Nov 2025 17:44:11 +0000 Subject: [PATCH 01/10] Added lines to check cases and return count of characters. --- Sprint-3/2-practice-tdd/count.js | 20 +++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) diff --git a/Sprint-3/2-practice-tdd/count.js b/Sprint-3/2-practice-tdd/count.js index 95b6ebb7d..2e461282d 100644 --- a/Sprint-3/2-practice-tdd/count.js +++ b/Sprint-3/2-practice-tdd/count.js @@ -1,5 +1,23 @@ function countChar(stringOfCharacters, findCharacter) { - return 5 + if (typeof stringOfCharacters!= "string") { + throw new Error("Input should be a string"); + } + if (typeof findCharacter !== "string" || findCharacter.length != 1){ + throw new Error ("Input must be a single character"); + } + + let count = 0; + for(let char of stringOfCharacters) { + if (char === findCharacter) { + count++; + } + } + return count; + } module.exports = countChar; + +console.log(countChar("amazon", "a")); + +// Added lines to check cases and return count of characters. From 826a780eef21f0413de0a46b7ae14131229c7397 Mon Sep 17 00:00:00 2001 From: UbunMen Date: Wed, 12 Nov 2025 17:10:03 +0000 Subject: [PATCH 02/10] Added different cases and test using npx jest --- Sprint-3/2-practice-tdd/count.test.js | 52 ++++++++++++++++++++++++--- 1 file changed, 48 insertions(+), 4 deletions(-) diff --git a/Sprint-3/2-practice-tdd/count.test.js b/Sprint-3/2-practice-tdd/count.test.js index 42baf4b4b..d5bf69460 100644 --- a/Sprint-3/2-practice-tdd/count.test.js +++ b/Sprint-3/2-practice-tdd/count.test.js @@ -18,7 +18,51 @@ test("should count multiple occurrences of a character", () => { }); // Scenario: No Occurrences -// Given the input string str, -// And a character char that does not exist within the case-sensitive str, -// When the function is called with these inputs, -// Then it should return 0, indicating that no occurrences of the char were found in the case-sensitive str. +test("should return 0 when the character is not in the string", () => { + const str = "hello"; + const char = "x"; + const count = countChar(str, char); + expect(count).toEqual(0); +}); + +// Scenario: Case Sensitivity + +test("should be case-sensitive when counting characters", () => { + const str = "Banana"; + expect(countChar(str, "a")).toEqual(3); + expect(countChar(str, "A")).toEqual(0); +}); + +// Scenario: Empty String + +test("should return 0 when the input string is empty", () => { + const str = ""; + const char = "a"; + const count = countChar(str, char); + expect(count).toEqual(0); +}); + +// Scenario : Special characters + +test("should count special characters like spaces or punctuation", () => { + const str1 = "a b a b"; + const char1 = " "; + expect(countChar(str1, char1)).toEqual(2); + + const str2 = "NO !!!"; + const char2 = "!"; + expect(countChar(str2, char2)).toEqual(3); +}); + +// Scenario: Invalid inputs + +test("should throw error for invalid string input", () => { + expect(() => countChar(123, "a")).toThrow("Input should be a string"); +}); + +test("should throw error for invalid character input", () => { + expect(() => countChar("hello", "ab")).toThrow("Input must be a single character"); +}); + + +// Added different cases and test using npx jest \ No newline at end of file From a1a791ebeba3f03e0c94b925b5718736879b16dd Mon Sep 17 00:00:00 2001 From: UbunMen Date: Wed, 12 Nov 2025 17:53:35 +0000 Subject: [PATCH 03/10] Function implemented --- Sprint-3/2-practice-tdd/get-ordinal-number.js | 22 ++++++++++++++++++- 1 file changed, 21 insertions(+), 1 deletion(-) diff --git a/Sprint-3/2-practice-tdd/get-ordinal-number.js b/Sprint-3/2-practice-tdd/get-ordinal-number.js index f95d71db1..be58bc8e9 100644 --- a/Sprint-3/2-practice-tdd/get-ordinal-number.js +++ b/Sprint-3/2-practice-tdd/get-ordinal-number.js @@ -1,5 +1,25 @@ function getOrdinalNumber(num) { - return "1st"; + if (!Number.isInteger(num) || num <= 0) { + throw new Error("Only positive integers are allowed"); + } + // Handle Exception of 11, 12, 13 + if (num % 100 >= 11 && num % 100 <= 13) { + return num + "th"; + } + // Handle general last-digit cases + switch (num % 10) { + case 1: + return num + "st"; + case 2: + return num + "nd"; + case 3: + return num + "rd"; + default: + return num + "th"; + } } + module.exports = getOrdinalNumber; + +// Function implemented From 5b62a723ec2e8d9b864aeb0214b9fda35d0a5bf7 Mon Sep 17 00:00:00 2001 From: UbunMen Date: Wed, 12 Nov 2025 17:54:47 +0000 Subject: [PATCH 04/10] Added possible and invalid cases and tested using npx jest --- .../2-practice-tdd/get-ordinal-number.test.js | 35 ++++++++++++++++++- 1 file changed, 34 insertions(+), 1 deletion(-) diff --git a/Sprint-3/2-practice-tdd/get-ordinal-number.test.js b/Sprint-3/2-practice-tdd/get-ordinal-number.test.js index dfe4b6091..2eabad621 100644 --- a/Sprint-3/2-practice-tdd/get-ordinal-number.test.js +++ b/Sprint-3/2-practice-tdd/get-ordinal-number.test.js @@ -8,6 +8,39 @@ const getOrdinalNumber = require("./get-ordinal-number"); // When the number is 1, // Then the function should return "1st" -test("should return '1st' for 1", () => { +// Case 1: 1 → Standard ordinals + +test("should return correct ordinals for 1, 2, 3, 4, 21, 22, 23, 24", () => { expect(getOrdinalNumber(1)).toEqual("1st"); + expect(getOrdinalNumber(2)).toEqual("2nd"); + expect(getOrdinalNumber(3)).toEqual("3rd"); + expect(getOrdinalNumber(4)).toEqual("4th"); + expect(getOrdinalNumber(21)).toEqual("21st"); + expect(getOrdinalNumber(22)).toEqual("22nd"); + expect(getOrdinalNumber(23)).toEqual("23rd"); + expect(getOrdinalNumber(24)).toEqual("24th"); +}); + +// Case 2: Exceptions 11, 12, 13 +test("should handle exceptions 11, 12, 13 correctly", () => { + expect(getOrdinalNumber(11)).toEqual("11th"); + expect(getOrdinalNumber(12)).toEqual("12th"); + expect(getOrdinalNumber(13)).toEqual("13th"); + expect(getOrdinalNumber(111)).toEqual("111th"); +}); + +// Case 3: Large numbers +test("should handle larger numbers correctly", () => { + expect(getOrdinalNumber(101)).toEqual("101st"); + expect(getOrdinalNumber(112)).toEqual("112th"); + expect(getOrdinalNumber(123)).toEqual("123rd"); +}); + +// Case 4: Invalid cases / inputs +test("should throw error for zero, negative or non-integer inputs", () => { + expect(() => getOrdinalNumber(0)).toThrow("Only positive integers are allowed"); + expect(() => getOrdinalNumber(-5)).toThrow("Only positive integers are allowed"); + expect(() => getOrdinalNumber(2.5)).toThrow("Only positive integers are allowed"); }); + +// Added possible and invalid cases and tested using npx jest \ No newline at end of file From b3404b7f9c588eed9b8f9c3eb2aba5e7f537f142 Mon Sep 17 00:00:00 2001 From: UbunMen Date: Wed, 12 Nov 2025 18:44:11 +0000 Subject: [PATCH 05/10] Tested for all case using npx jest --- Sprint-3/2-practice-tdd/repeat.test.js | 41 ++++++++++++++++++-------- 1 file changed, 29 insertions(+), 12 deletions(-) diff --git a/Sprint-3/2-practice-tdd/repeat.test.js b/Sprint-3/2-practice-tdd/repeat.test.js index 34097b09c..ce5dd7660 100644 --- a/Sprint-3/2-practice-tdd/repeat.test.js +++ b/Sprint-3/2-practice-tdd/repeat.test.js @@ -9,6 +9,8 @@ const repeat = require("./repeat"); // When the repeat function is called with these inputs, // Then it should repeat the str count times and return a new string containing the repeated str values. +// Case 1: Repeats String coun times. + test("should repeat the string count times", () => { const str = "hello"; const count = 3; @@ -16,17 +18,32 @@ test("should repeat the string count times", () => { expect(repeatedStr).toEqual("hellohellohello"); }); -// case: handle Count of 1: -// Given a target string str and a count equal to 1, -// When the repeat function is called with these inputs, -// Then it should return the original str without repetition, ensuring that a count of 1 results in no repetition. +// case 2: handle Count of 1: -// case: Handle Count of 0: -// Given a target string str and a count equal to 0, -// When the repeat function is called with these inputs, -// Then it should return an empty string, ensuring that a count of 0 results in an empty output. +test("should return the original string when count is 1", () => { + const str = "hello"; + const count = 1; + expect(repeat(str, count)).toEqual("hello"); +}); +// Returns the original str without repetition, ensuring that a count of 1 results in no repetition. -// case: Negative Count: -// Given a target string str and a negative integer count, -// When the repeat function is called with these inputs, -// Then it should throw an error or return an appropriate error message, as negative counts are not valid. +// case 3: Handle Count of 0: + +test("should return an empty string when count is 0", () => { + const str = "hello"; + const count = 0; + expect(repeat(str, count)).toEqual(""); +}); + +// Returns an empty string, ensuring that a count of 0 results in an empty output. + +// Case 4: Negative count + +test("should throw an error when count is negative", () => { + const str = "hello"; + const count = -2; + expect(() => repeat(str, count)).toThrow("Count must be a non-negative integer"); +}); +// Throws an error or return an appropriate error message, as negative counts are not valid. + +// Tested for all case using npx jest \ No newline at end of file From 452b1a6cb5bbe0d60c7152ae8ee7c4c8943f1389 Mon Sep 17 00:00:00 2001 From: UbunMen Date: Wed, 12 Nov 2025 18:44:53 +0000 Subject: [PATCH 06/10] Function implemented to repeat string count times. --- Sprint-3/2-practice-tdd/repeat.js | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/Sprint-3/2-practice-tdd/repeat.js b/Sprint-3/2-practice-tdd/repeat.js index 00e60d7f3..b4aaa36c2 100644 --- a/Sprint-3/2-practice-tdd/repeat.js +++ b/Sprint-3/2-practice-tdd/repeat.js @@ -1,5 +1,15 @@ -function repeat() { - return "hellohellohello"; +function repeat(str, count) { + if (typeof str !== "string") { + throw new Error("Input must be a string"); + } + if (!Number.isInteger(count) || count < 0) { + throw new Error("Count must be a non-negative integer"); + } + + return str.repeat(count); } module.exports = repeat; + + +// Function implemented to repeat string count times. \ No newline at end of file From 9dc5687cde4d563074ad3b634a8140197236a25f Mon Sep 17 00:00:00 2001 From: UbunMen Date: Thu, 13 Nov 2025 00:52:16 +0000 Subject: [PATCH 07/10] Added explanation to the questions. --- Sprint-3/3-stretch/find.js | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/Sprint-3/3-stretch/find.js b/Sprint-3/3-stretch/find.js index c7e79a2f2..2eea202db 100644 --- a/Sprint-3/3-stretch/find.js +++ b/Sprint-3/3-stretch/find.js @@ -19,7 +19,23 @@ console.log(find("code your future", "z")); // Use the Python Visualiser to help you play computer with this example and observe how this code is executed // Pay particular attention to the following: -// a) How the index variable updates during the call to find +// a) How the index variable updates during the call to find a char + + // As the code runs, index starts from zero and runs until the length of the string, + // It starts from zero and in each loop it gets incremented until a char is found within the string. + // b) What is the if statement used to check + + // It is used to check if the char is in the string. If it exists.. next step is to return the char. + + // c) Why is index++ being used? + + // To check if the given char is present starting from 0 till st.length. + + // d) What is the condition index < str.length used for? + + // As long as index is less than the length of the string, keep looping and finding the char. + +// Added explanation to the questions. \ No newline at end of file From 3910418a1f34f999e882e0a88e08887871d56b58 Mon Sep 17 00:00:00 2001 From: UbunMen Date: Sat, 15 Nov 2025 17:44:32 +0000 Subject: [PATCH 08/10] Modified explanation --- Sprint-3/3-stretch/find.js | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/Sprint-3/3-stretch/find.js b/Sprint-3/3-stretch/find.js index 2eea202db..e8ff08dc6 100644 --- a/Sprint-3/3-stretch/find.js +++ b/Sprint-3/3-stretch/find.js @@ -26,16 +26,16 @@ console.log(find("code your future", "z")); // b) What is the if statement used to check - // It is used to check if the char is in the string. If it exists.. next step is to return the char. + // It checks if the char is in the string. If it exists.. next step is to return the index of the char. // c) Why is index++ being used? - // To check if the given char is present starting from 0 till st.length. + // To check if the given char is present starting from 0 till str.length. // d) What is the condition index < str.length used for? - // As long as index is less than the length of the string, keep looping and finding the char. + // As long as index is less than the length of the string, keep looping until the char is found. // Added explanation to the questions. \ No newline at end of file From de4bc3e0ebf25c4bee26dd3570ad848cacf2a39d Mon Sep 17 00:00:00 2001 From: UbunMen Date: Sat, 15 Nov 2025 19:25:48 +0000 Subject: [PATCH 09/10] Password-validator implemented and tested. --- Sprint-3/3-stretch/password-validator.js | 9 ++- Sprint-3/3-stretch/password-validator.test.js | 56 ++++++++++++++++++- 2 files changed, 63 insertions(+), 2 deletions(-) diff --git a/Sprint-3/3-stretch/password-validator.js b/Sprint-3/3-stretch/password-validator.js index b55d527db..acf0f3d33 100644 --- a/Sprint-3/3-stretch/password-validator.js +++ b/Sprint-3/3-stretch/password-validator.js @@ -1,5 +1,12 @@ function passwordValidator(password) { - return password.length < 5 ? false : true + // return password.length < 5 ? false : true + + return (password.length < 5 && + /[A-Za-z]/.test(password)&& + /[! # $ % .* &]/.test(password)&& + /[0-9]/.test(password)&& + !previousPasswords.includes(password) + ); } diff --git a/Sprint-3/3-stretch/password-validator.test.js b/Sprint-3/3-stretch/password-validator.test.js index 8fa3089d6..39fe3be31 100644 --- a/Sprint-3/3-stretch/password-validator.test.js +++ b/Sprint-3/3-stretch/password-validator.test.js @@ -15,6 +15,7 @@ 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", () => { // Arrange const password = "12345"; @@ -23,4 +24,57 @@ test("password has at least 5 characters", () => { // Assert expect(result).toEqual(true); } -); \ No newline at end of file +); + +// +test("password has at least one English uppercase letter (A-Z)", () => { + // Arrange + const password = "A2345"; + // Act + const result = isValidPassword(password); + // Assert + expect(result).toEqual(true); +} +); + +// +test("password has least one English lowercase letter (a-z)", () => { + // Arrange + const password = "Da2345"; + // Act + const result = isValidPassword(password); + // Assert + expect(result).toEqual(true); +} +); + +// +test("password has at least one number 0-9)", () => { + // Arrange + const password = "Cz!345"; + // Act + const result = isValidPassword(password); + // Assert + expect(result).toEqual(true); +} +); + +test("password has at least one non-alphanumeric symbols: !, #, $, %, ., *, &)", () => { + // Arrange + const password = "Cz!345"; + // Act + const result = isValidPassword(password); + // Assert + expect(result).toEqual(true); +} +); + +// +test("password must not be a previous password", () => { + const previousPasswords = ["Mmd1!", "XyZ2$", "Test10%"]; + const password = "Mmd1"; + const result = isValidPassword(password, previousPasswords); + expect(result).toEqual(false); +}); + +// Password-validator implemented and tested. \ No newline at end of file From e0b89365c6aa9bc4af2ae63edc2c1591a59112e0 Mon Sep 17 00:00:00 2001 From: UbunMen Date: Sat, 15 Nov 2025 20:11:46 +0000 Subject: [PATCH 10/10] changes made, implemented and tested. --- Sprint-3/3-stretch/password-validator.js | 20 ++--- Sprint-3/3-stretch/password-validator.test.js | 81 +++++++------------ 2 files changed, 40 insertions(+), 61 deletions(-) diff --git a/Sprint-3/3-stretch/password-validator.js b/Sprint-3/3-stretch/password-validator.js index acf0f3d33..74bd9e444 100644 --- a/Sprint-3/3-stretch/password-validator.js +++ b/Sprint-3/3-stretch/password-validator.js @@ -1,13 +1,13 @@ -function passwordValidator(password) { - // return password.length < 5 ? false : true - - return (password.length < 5 && - /[A-Za-z]/.test(password)&& - /[! # $ % .* &]/.test(password)&& - /[0-9]/.test(password)&& - !previousPasswords.includes(password) - ); +function isValidPassword(password, previousPasswords = []) { + return ( + password.length >= 5 && + /[A-Z]/.test(password) && + /[a-z]/.test(password) && + /[0-9]/.test(password) && + /[!#$%.*&]/.test(password) && + !previousPasswords.includes(password) + ); } -module.exports = passwordValidator; \ No newline at end of file +module.exports = isValidPassword; \ No newline at end of file diff --git a/Sprint-3/3-stretch/password-validator.test.js b/Sprint-3/3-stretch/password-validator.test.js index 39fe3be31..f2f946e83 100644 --- a/Sprint-3/3-stretch/password-validator.test.js +++ b/Sprint-3/3-stretch/password-validator.test.js @@ -15,66 +15,45 @@ 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"); +const previousPasswords = ["Mmd1!", "XyZ2$", "Tes5%"]; + test("password has at least 5 characters", () => { - // Arrange - const password = "12345"; - // Act - const result = isValidPassword(password); - // Assert - expect(result).toEqual(true); -} -); + const password = "Ki55$"; + const result = isValidPassword(password, previousPasswords); // pass the array + expect(result).toEqual(true); +}); -// -test("password has at least one English uppercase letter (A-Z)", () => { - // Arrange - const password = "A2345"; - // Act - const result = isValidPassword(password); - // Assert - expect(result).toEqual(true); -} -); +test("password has at least one uppercase", () => { + const password = "Uo85*"; + const result = isValidPassword(password, previousPasswords); + expect(result).toEqual(true);npx +}); -// -test("password has least one English lowercase letter (a-z)", () => { - // Arrange - const password = "Da2345"; - // Act - const result = isValidPassword(password); - // Assert - expect(result).toEqual(true); -} -); +test("password has at least one lowercase", () => { + const password = "Qf#45"; + const result = isValidPassword(password, previousPasswords); + expect(result).toEqual(true); +}); -// -test("password has at least one number 0-9)", () => { - // Arrange - const password = "Cz!345"; - // Act - const result = isValidPassword(password); - // Assert - expect(result).toEqual(true); -} -); +test("password has at least one number", () => { + const password = "Cz!35"; + const result = isValidPassword(password, previousPasswords); + expect(result).toEqual(true); +}); -test("password has at least one non-alphanumeric symbols: !, #, $, %, ., *, &)", () => { - // Arrange - const password = "Cz!345"; - // Act - const result = isValidPassword(password); - // Assert - expect(result).toEqual(true); -} -); +test("password has at least one special symbol", () => { + const password = "Re*19"; + const result = isValidPassword(password, previousPasswords); + expect(result).toEqual(true); +}); -// test("password must not be a previous password", () => { - const previousPasswords = ["Mmd1!", "XyZ2$", "Test10%"]; - const password = "Mmd1"; + const password = "Mmd1!"; const result = isValidPassword(password, previousPasswords); expect(result).toEqual(false); + }); -// Password-validator implemented and tested. \ No newline at end of file + +// Password-validator implemented and tested.