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 eb7c0eb2f01cebda7693f414aafc14052f5749db Mon Sep 17 00:00:00 2001 From: UbunMen Date: Sun, 16 Nov 2025 01:21:43 +0000 Subject: [PATCH 07/10] Indentation improved. --- Sprint-3/2-practice-tdd/count.js | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/Sprint-3/2-practice-tdd/count.js b/Sprint-3/2-practice-tdd/count.js index 2e461282d..893056bf3 100644 --- a/Sprint-3/2-practice-tdd/count.js +++ b/Sprint-3/2-practice-tdd/count.js @@ -7,13 +7,13 @@ function countChar(stringOfCharacters, findCharacter) { } let count = 0; + for(let char of stringOfCharacters) { if (char === findCharacter) { count++; } } return count; - } module.exports = countChar; @@ -21,3 +21,4 @@ module.exports = countChar; console.log(countChar("amazon", "a")); // Added lines to check cases and return count of characters. +// Indentation improved. From 4b42c9ff46648267bcd308617edc46c674a5cd12 Mon Sep 17 00:00:00 2001 From: UbunMen Date: Sun, 16 Nov 2025 16:06:37 +0000 Subject: [PATCH 08/10] Added some tests, additional test and passed the tests. --- Sprint-3/2-practice-tdd/count.test.js | 22 ++++++++++++++++++++-- 1 file changed, 20 insertions(+), 2 deletions(-) diff --git a/Sprint-3/2-practice-tdd/count.test.js b/Sprint-3/2-practice-tdd/count.test.js index d5bf69460..0e99fd0f0 100644 --- a/Sprint-3/2-practice-tdd/count.test.js +++ b/Sprint-3/2-practice-tdd/count.test.js @@ -47,13 +47,29 @@ test("should return 0 when the input string is empty", () => { test("should count special characters like spaces or punctuation", () => { const str1 = "a b a b"; const char1 = " "; - expect(countChar(str1, char1)).toEqual(2); + expect(countChar(str1, char1)).toEqual(3); const str2 = "NO !!!"; const char2 = "!"; expect(countChar(str2, char2)).toEqual(3); }); +// Scenario: Numeric characters inside string + +test("should count numeric characters inside the string", () => { + const str3 = "123321"; + const char3 = "2"; + expect(countChar(str3, char3)).toEqual(2); +}); + +// Scenarion Accented characters + +test("should count accented characters and unicode properly", () => { + const str4 = "café"; + const char4 = "é"; + expect(countChar(str4, char4)).toEqual(1); + }); + // Scenario: Invalid inputs test("should throw error for invalid string input", () => { @@ -65,4 +81,6 @@ test("should throw error for invalid character input", () => { }); -// Added different cases and test using npx jest \ No newline at end of file +// Added different cases and test using npx jest + +// Added some tests, additional test and passed the tests. \ No newline at end of file From 6993bb1559c6e3b72247aad5c1aff13be5aed8a5 Mon Sep 17 00:00:00 2001 From: UbunMen Date: Sun, 16 Nov 2025 16:22:20 +0000 Subject: [PATCH 09/10] Modified the test into groups --- .../2-practice-tdd/get-ordinal-number.test.js | 66 ++++++++++++------- 1 file changed, 42 insertions(+), 24 deletions(-) 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 2eabad621..a839ff306 100644 --- a/Sprint-3/2-practice-tdd/get-ordinal-number.test.js +++ b/Sprint-3/2-practice-tdd/get-ordinal-number.test.js @@ -8,39 +8,57 @@ const getOrdinalNumber = require("./get-ordinal-number"); // When the number is 1, // Then the function should return "1st" -// 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 1: Numbers ending in 1 except 11 + +test("should return 'st' for numbers ending in 1 except 11", () => { + const values = [1, 21, 31, 101, 1001]; + values.forEach(num => { + expect(getOrdinalNumber(num)).toEqual(`${num}st`); + }); }); -// 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 2: Numbers ending in 2 except 12 + +test("should return 'nd' for numbers ending in 2 except 12", () => { + const values = [2, 22, 32, 102, 1002]; + values.forEach(num => { + expect(getOrdinalNumber(num)).toEqual(`${num}nd`); + }); +}); + +// Case 3: Numbers ending in 3 except 13 + +test("should return 'rd' for numbers ending in 3 except 13", () => { + const values = [3, 23, 33, 103, 1003]; + values.forEach(num => { + expect(getOrdinalNumber(num)).toEqual(`${num}rd`); + }); }); -// 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: Exceptions 11, 12 & 13 + +test("should return 'th' for 11, 12, 13 regardless of other digits", () => { + const values = [11, 12, 13, 111, 212, 513]; + values.forEach(num => { + expect(getOrdinalNumber(num)).toEqual(`${num}th`); + }); +}); + +// Case 5: Numbers ending in 0 and (4-9) + +test("should return 'th' for numbers ending in 0 or 4–9", () => { + const values = [4, 5, 6, 7, 8, 9, 10, 20, 25, 100, 104, 1009]; + values.forEach(num => { + expect(getOrdinalNumber(num)).toEqual(`${num}th`); + }); }); -// Case 4: Invalid cases / inputs +// Case 6: 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 +// Added possible and invalid cases and tested using npx jest +// Modified the test into groups \ No newline at end of file From 7cf20f29f38417f1f8604206d0626b3c08bb0eb8 Mon Sep 17 00:00:00 2001 From: UbunMen Date: Fri, 21 Nov 2025 15:13:41 +0000 Subject: [PATCH 10/10] count.js formatted with Prettier. --- Sprint-3/2-practice-tdd/count.js | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) diff --git a/Sprint-3/2-practice-tdd/count.js b/Sprint-3/2-practice-tdd/count.js index 893056bf3..a82d5ee79 100644 --- a/Sprint-3/2-practice-tdd/count.js +++ b/Sprint-3/2-practice-tdd/count.js @@ -1,19 +1,19 @@ function countChar(stringOfCharacters, findCharacter) { - if (typeof stringOfCharacters!= "string") { + 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"); + 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) { + for (let char of stringOfCharacters) { + if (char === findCharacter) { count++; - } - } - return count; + } + } + return count; } module.exports = countChar; @@ -22,3 +22,4 @@ console.log(countChar("amazon", "a")); // Added lines to check cases and return count of characters. // Indentation improved. +// count.js formatted with prettier.