From 19452c4359800f93d01b819e3a105db7e63a76ab Mon Sep 17 00:00:00 2001 From: "Karla G." Date: Wed, 25 Feb 2026 23:57:20 +0000 Subject: [PATCH 01/12] test: add cases for no occurrences and case sensitivity in countChar function --- Sprint-3/2-practice-tdd/count.test.js | 32 ++++++++++++++++++++++----- 1 file changed, 26 insertions(+), 6 deletions(-) diff --git a/Sprint-3/2-practice-tdd/count.test.js b/Sprint-3/2-practice-tdd/count.test.js index 179ea0ddf7..300ff6bf44 100644 --- a/Sprint-3/2-practice-tdd/count.test.js +++ b/Sprint-3/2-practice-tdd/count.test.js @@ -4,12 +4,23 @@ const countChar = require("./count"); // When the countChar function is called with these inputs, // Then it should: +// Scenario: No Occurrences +// Given the input string `str`, +// And a character `char` that does not exist within `str`. +// When the function is called with these inputs, +// Then it should return 0, indicating that no occurrences of `char` were found. +test("should return no occurrences if no found character", () => { + const str = "jhhfdd"; + const char = "r"; + const count = countChar(str, char); + expect(count).toEqual(0); + }); + // Scenario: Multiple Occurrences // Given the input string `str`, // And a character `char` that occurs one or more times in `str` (e.g., 'a' in 'aaaaa'), // When the function is called with these inputs, // Then it should correctly count occurrences of `char`. - test("should count multiple occurrences of a character", () => { const str = "aaaaa"; const char = "a"; @@ -17,8 +28,17 @@ test("should count multiple occurrences of a character", () => { expect(count).toEqual(5); }); -// Scenario: No Occurrences -// Given the input string `str`, -// And a character `char` that does not exist within `str`. -// When the function is called with these inputs, -// Then it should return 0, indicating that no occurrences of `char` were found. +// case 2 +test("should return 1 when the character appears only once", () => { + expect(countChar("hello", "h")).toEqual(1); +}); + +// case 3 +test("Should return 0 for empty string", () => { + expect(countChar("", "a")).toEqual(0) +}) + +// case 4 +test("should be case sensitive", () => { + expect(countChar("aA", "a")).toEqual(1); +}); \ No newline at end of file From b7cdfee7a1b4a271f3ecf40cdeeec722236a6c96 Mon Sep 17 00:00:00 2001 From: "Karla G." Date: Wed, 25 Feb 2026 23:57:35 +0000 Subject: [PATCH 02/12] feat: implement character counting function in count.js --- Sprint-3/2-practice-tdd/count.js | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/Sprint-3/2-practice-tdd/count.js b/Sprint-3/2-practice-tdd/count.js index 95b6ebb7d4..97395357b5 100644 --- a/Sprint-3/2-practice-tdd/count.js +++ b/Sprint-3/2-practice-tdd/count.js @@ -1,5 +1,15 @@ function countChar(stringOfCharacters, findCharacter) { - return 5 + + let count = 0; + + for (i = 0; i < stringOfCharacters.length; i++) { + if (stringOfCharacters[i] === findCharacter) { + count += 1; + } + } + + return count; } module.exports = countChar; + From a355524fa9577ab7d336ec2f15ba5ec8fb156d43 Mon Sep 17 00:00:00 2001 From: "Karla G." Date: Thu, 26 Feb 2026 16:57:11 +0000 Subject: [PATCH 03/12] test: add TDD cases for the function get-ordinal-numbers to add suffix --- .../2-practice-tdd/get-ordinal-number.test.js | 30 +++++++++++++++++++ 1 file changed, 30 insertions(+) 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 adfa58560f..658bb32452 100644 --- a/Sprint-3/2-practice-tdd/get-ordinal-number.test.js +++ b/Sprint-3/2-practice-tdd/get-ordinal-number.test.js @@ -18,3 +18,33 @@ test("should append 'st' for numbers ending with 1, except those ending with 11" expect(getOrdinalNumber(21)).toEqual("21st"); expect(getOrdinalNumber(131)).toEqual("131st"); }); + +// Caso 2: number ending in 2 (nd) - but no 12 +test("should append 'nd' for numbers ending with 2, except 12", () => { + expect(getOrdinalNumber(2)).toEqual("2nd"); + expect(getOrdinalNumber(22)).toEqual("22nd"); + expect(getOrdinalNumber(102)).toEqual("102nd"); +}); + +// case 3: numbers ending on 3 but no 13 +test("Should append 'rd' for numbers ending with 3, except 13", () => { + expect(getOrdinalNumber(23)).toEqual("23rd"); + expect(getOrdinalNumber(33)).toEqual("33rd"); +}); + +// case 4: numbers exception like 11, 12 and 13 append th +test("should append 'th' for the exceptions 11, 12, and 13", () => { + expect(getOrdinalNumber(11)).toEqual("11th"); + expect(getOrdinalNumber(12)).toEqual("12th"); + expect(getOrdinalNumber(13)).toEqual("13th"); +}); + +// case 5: rest of numbers +test("should append 'th' for number ending in 4, 5, 6, 7, 8, 9 or 0", () => { + expect(getOrdinalNumber(4)).toEqual("4th"); + expect(getOrdinalNumber(6)).toEqual("6th"); + expect(getOrdinalNumber(17)).toEqual("17th"); + expect(getOrdinalNumber(18)).toEqual("18th"); + expect(getOrdinalNumber(29)).toEqual("29th"); + expect(getOrdinalNumber(30)).toEqual("30th"); +}); From 2e25623dd4c51d9fc7c61537bd20a4554915c7b4 Mon Sep 17 00:00:00 2001 From: "Karla G." Date: Thu, 26 Feb 2026 16:58:32 +0000 Subject: [PATCH 04/12] feat: add ordinal suffix handling including special cases for 11, 12, and 13 --- Sprint-3/2-practice-tdd/get-ordinal-number.js | 16 +++++++++++++++- 1 file changed, 15 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 f95d71db13..aabb2f4bc9 100644 --- a/Sprint-3/2-practice-tdd/get-ordinal-number.js +++ b/Sprint-3/2-practice-tdd/get-ordinal-number.js @@ -1,5 +1,19 @@ function getOrdinalNumber(num) { - return "1st"; + + const lastTwoDigits = num % 100; + const lastDigit = num % 10 + + + if (lastTwoDigits === 11 || lastTwoDigits === 12 || lastTwoDigits === 13) { + return num + "th"; + } + + + if(lastDigit === 1) return num + "st"; + if(lastDigit === 2) return num + "nd"; + if(lastDigit === 3) return num + "rd"; + + return num + "th" } module.exports = getOrdinalNumber; From b498ab0f57009f1d1f9d5a22b0bbd09b38782d90 Mon Sep 17 00:00:00 2001 From: "Karla G." Date: Thu, 26 Feb 2026 22:15:49 +0000 Subject: [PATCH 05/12] test: add cases for count of 0 and negative count in repeatStr function --- Sprint-3/2-practice-tdd/repeat-str.test.js | 27 ++++++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/Sprint-3/2-practice-tdd/repeat-str.test.js b/Sprint-3/2-practice-tdd/repeat-str.test.js index a3fc1196c4..d0d0f791e6 100644 --- a/Sprint-3/2-practice-tdd/repeat-str.test.js +++ b/Sprint-3/2-practice-tdd/repeat-str.test.js @@ -4,6 +4,8 @@ const repeatStr = require("./repeat-str"); // When the repeatStr function is called with these inputs, // Then it should: +// Clean Code example, Gherkin style (Given/When/Then) + // Case: handle multiple repetitions: // Given a target string `str` and a positive integer `count` greater than 1, // When the repeatStr function is called with these inputs, @@ -20,13 +22,38 @@ test("should repeat the string count times", () => { // Given a target string `str` and a `count` equal to 1, // When the repeatStr function is called with these inputs, // Then it should return the original `str` without repetition. +test("Should return the original string if count is 1", () => { + expect(repeatStr("singleStf", 1)).toBe("singleStf"); +}); + // Case: Handle count of 0: // Given a target string `str` and a `count` equal to 0, // When the repeatStr function is called with these inputs, // Then it should return an empty string. +test("Should return an empty string if the count is 0", () => { + expect(repeatStr("hello", 0)).toBe(""); +}); // Case: Handle negative count: // Given a target string `str` and a negative integer `count`, // When the repeatStr function is called with these inputs, // Then it should throw an error, as negative counts are not valid. +test("Should throw an error if the count is negative", () => { + expect(() => repeatStr("hi", -3)).toThrow(); +}); + +function repeatStr(str, count) { + if (count < 0) { + throw new Error("count cannot be negative"); + } + if (count === 1) return str; + + let result = ""; + for (let i = 0; i < count; i++) { + result = result + str; + } + return result; +} + +module.exports = repeatStr; \ No newline at end of file From 11a1b988c9f4af16ec14e5a3bf996655fa56a5d3 Mon Sep 17 00:00:00 2001 From: "Karla G." Date: Thu, 26 Feb 2026 22:18:05 +0000 Subject: [PATCH 06/12] feat: enhance repeatStr function to handle negative counts and improve string repetition logic --- Sprint-3/2-practice-tdd/repeat-str.js | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/Sprint-3/2-practice-tdd/repeat-str.js b/Sprint-3/2-practice-tdd/repeat-str.js index 3838c7b003..09e2c2edd9 100644 --- a/Sprint-3/2-practice-tdd/repeat-str.js +++ b/Sprint-3/2-practice-tdd/repeat-str.js @@ -1,5 +1,15 @@ -function repeatStr() { - return "hellohellohello"; +function repeatStr(str, count) { + if (count < 0) { + throw new Error("count cannot be negative"); + } + if (count === 1) return str; + + let result = ""; + for (let i = 0; i < count; i++) { + result = result + str; + } + return result; } module.exports = repeatStr; + From 4c0130e66687d63c4360480dcd64acfa849a2efe Mon Sep 17 00:00:00 2001 From: "Karla G." Date: Thu, 26 Feb 2026 22:19:16 +0000 Subject: [PATCH 07/12] refactor: simplify repeatStr function by removing redundant check for count of 1 --- Sprint-3/2-practice-tdd/repeat-str.js | 3 +-- Sprint-3/2-practice-tdd/repeat-str.test.js | 15 --------------- 2 files changed, 1 insertion(+), 17 deletions(-) diff --git a/Sprint-3/2-practice-tdd/repeat-str.js b/Sprint-3/2-practice-tdd/repeat-str.js index 09e2c2edd9..49e552438c 100644 --- a/Sprint-3/2-practice-tdd/repeat-str.js +++ b/Sprint-3/2-practice-tdd/repeat-str.js @@ -2,8 +2,7 @@ function repeatStr(str, count) { if (count < 0) { throw new Error("count cannot be negative"); } - if (count === 1) return str; - + let result = ""; for (let i = 0; i < count; i++) { result = result + str; diff --git a/Sprint-3/2-practice-tdd/repeat-str.test.js b/Sprint-3/2-practice-tdd/repeat-str.test.js index d0d0f791e6..7bb8c2778b 100644 --- a/Sprint-3/2-practice-tdd/repeat-str.test.js +++ b/Sprint-3/2-practice-tdd/repeat-str.test.js @@ -42,18 +42,3 @@ test("Should return an empty string if the count is 0", () => { test("Should throw an error if the count is negative", () => { expect(() => repeatStr("hi", -3)).toThrow(); }); - -function repeatStr(str, count) { - if (count < 0) { - throw new Error("count cannot be negative"); - } - if (count === 1) return str; - - let result = ""; - for (let i = 0; i < count; i++) { - result = result + str; - } - return result; -} - -module.exports = repeatStr; \ No newline at end of file From b548ddb61b10c3f64ea0c38137389eb1ce7d4eea Mon Sep 17 00:00:00 2001 From: "Karla G." Date: Thu, 26 Feb 2026 22:43:37 +0000 Subject: [PATCH 08/12] feat: update negative count error message in repeatStr - Added an emoji to the error string. - Included try/catch blocks in tests to verify manual error capturing. - Updated Jest assertions to match the new error format. --- Sprint-3/2-practice-tdd/repeat-str.js | 21 +++++++++++++++++++-- 1 file changed, 19 insertions(+), 2 deletions(-) diff --git a/Sprint-3/2-practice-tdd/repeat-str.js b/Sprint-3/2-practice-tdd/repeat-str.js index 49e552438c..c4083a352f 100644 --- a/Sprint-3/2-practice-tdd/repeat-str.js +++ b/Sprint-3/2-practice-tdd/repeat-str.js @@ -1,8 +1,8 @@ function repeatStr(str, count) { if (count < 0) { - throw new Error("count cannot be negative"); + throw new Error("count cannot be negative 😭"); } - + let result = ""; for (let i = 0; i < count; i++) { result = result + str; @@ -12,3 +12,20 @@ function repeatStr(str, count) { module.exports = repeatStr; + +// Case: Manual inspection of the Error object +// We use try/catch to log the error message to the console +// to verify it's working as expected without breaking the test suite. +test("Should log the specific error message to the console", () => { + try { + repeatStr("hi", -3); + } catch (error) { + console.log("Verified Error Message ->", error.message); + + // Check if the message is exactly what we defined + expect(error.message).toBe("count cannot be negative 😭"); + } + + // Also verify that it actually throws using Jest's built-in matcher + expect(() => repeatStr("hi", -3)).toThrow(); +}); From b00a6cb4a94daefc2bd3ffcd2631b8b64ed28039 Mon Sep 17 00:00:00 2001 From: "Karla G." Date: Fri, 6 Mar 2026 22:14:11 +0000 Subject: [PATCH 09/12] test: add cases for non-alphabet characters in countChar function --- Sprint-3/2-practice-tdd/count.test.js | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/Sprint-3/2-practice-tdd/count.test.js b/Sprint-3/2-practice-tdd/count.test.js index 300ff6bf44..ce23a3b7ae 100644 --- a/Sprint-3/2-practice-tdd/count.test.js +++ b/Sprint-3/2-practice-tdd/count.test.js @@ -41,4 +41,13 @@ test("Should return 0 for empty string", () => { // case 4 test("should be case sensitive", () => { expect(countChar("aA", "a")).toEqual(1); -}); \ No newline at end of file +}); + +//case 5 Non-alphabet characters +test(`Should work with numbers and symbols`, () => { + expect(countChar("123451234123!&", "1")).toEqual(3); + expect(countChar("@1hello@", "@")).toEqual(2); + expect(countChar("white space ", " ")).toEqual(2); + expect(countChar("-2534!-1322!", "!")).toEqual(2); + expect(countChar("-hello ", "-")).toEqual(1); +}) From e04af18a6740e8a9dba1ae4ff9b758299ae3d5b8 Mon Sep 17 00:00:00 2001 From: "Karla G." Date: Fri, 6 Mar 2026 22:26:01 +0000 Subject: [PATCH 10/12] refactor: simplify repeatStr function using built-in string method --- Sprint-3/2-practice-tdd/repeat-str.js | 30 ++++++++++++++++++++------- 1 file changed, 22 insertions(+), 8 deletions(-) diff --git a/Sprint-3/2-practice-tdd/repeat-str.js b/Sprint-3/2-practice-tdd/repeat-str.js index c4083a352f..756c0c8078 100644 --- a/Sprint-3/2-practice-tdd/repeat-str.js +++ b/Sprint-3/2-practice-tdd/repeat-str.js @@ -1,15 +1,29 @@ +// function repeatStr(str, count) { +// if (count < 0) { +// throw new Error("count cannot be negative"); +// } + +// let result = ""; +// for (let i = 0; i < count; i++) { +// result = result + str; +// } +// return result; +// } + +// The repeat() method of String values constructs and returns +// a new string which contains the specified number of copies +// of this string, concatenated together. +//repeatStr("abc", 3); // "abcabcabc" + +//refactored function using built-in method function repeatStr(str, count) { if (count < 0) { - throw new Error("count cannot be negative 😭"); + throw new Error("count cannot be negative"); } - - let result = ""; - for (let i = 0; i < count; i++) { - result = result + str; - } - return result; + return str.repeat(count); } + module.exports = repeatStr; @@ -23,7 +37,7 @@ test("Should log the specific error message to the console", () => { console.log("Verified Error Message ->", error.message); // Check if the message is exactly what we defined - expect(error.message).toBe("count cannot be negative 😭"); + expect(error.message).toBe("count cannot be negative"); } // Also verify that it actually throws using Jest's built-in matcher From 49a3b76af8bf772591334a5ed9aed0239b26f431 Mon Sep 17 00:00:00 2001 From: "Karla G." Date: Fri, 6 Mar 2026 22:47:07 +0000 Subject: [PATCH 11/12] refactor: remove manual try/catch because Jest covers this case --- Sprint-3/2-practice-tdd/repeat-str.js | 31 ++++++++++++++------------- 1 file changed, 16 insertions(+), 15 deletions(-) diff --git a/Sprint-3/2-practice-tdd/repeat-str.js b/Sprint-3/2-practice-tdd/repeat-str.js index 756c0c8078..7334830020 100644 --- a/Sprint-3/2-practice-tdd/repeat-str.js +++ b/Sprint-3/2-practice-tdd/repeat-str.js @@ -27,19 +27,20 @@ function repeatStr(str, count) { module.exports = repeatStr; -// Case: Manual inspection of the Error object -// We use try/catch to log the error message to the console -// to verify it's working as expected without breaking the test suite. -test("Should log the specific error message to the console", () => { - try { - repeatStr("hi", -3); - } catch (error) { - console.log("Verified Error Message ->", error.message); - - // Check if the message is exactly what we defined - expect(error.message).toBe("count cannot be negative"); - } - // Also verify that it actually throws using Jest's built-in matcher - expect(() => repeatStr("hi", -3)).toThrow(); -}); + + +// // Case: Manual inspection of the Error object +// // We use try/catch to log the error message to the console +// // to verify it's working as expected without breaking the test suite. +// test("Should log the specific error message to the console", () => { +// try { +// repeatStr("hi", -3); +// } catch (error) { +// console.log("Verified Error Message ->", error.message); + +// // Check if the message is exactly what we defined +// expect(error.message).toBe("count cannot be negative"); +// } + +// }); From 0af1ee05e3475d80ed69bdae36c6bf0c784d81da Mon Sep 17 00:00:00 2001 From: "Karla G." Date: Sat, 7 Mar 2026 20:13:02 +0000 Subject: [PATCH 12/12] refactor: streamline repeatStr function by removing commented code and utilizing built-in method --- Sprint-3/2-practice-tdd/repeat-str.js | 37 --------------------------- 1 file changed, 37 deletions(-) diff --git a/Sprint-3/2-practice-tdd/repeat-str.js b/Sprint-3/2-practice-tdd/repeat-str.js index 7334830020..eed30e6cc4 100644 --- a/Sprint-3/2-practice-tdd/repeat-str.js +++ b/Sprint-3/2-practice-tdd/repeat-str.js @@ -1,20 +1,3 @@ -// function repeatStr(str, count) { -// if (count < 0) { -// throw new Error("count cannot be negative"); -// } - -// let result = ""; -// for (let i = 0; i < count; i++) { -// result = result + str; -// } -// return result; -// } - -// The repeat() method of String values constructs and returns -// a new string which contains the specified number of copies -// of this string, concatenated together. -//repeatStr("abc", 3); // "abcabcabc" - //refactored function using built-in method function repeatStr(str, count) { if (count < 0) { @@ -23,24 +6,4 @@ function repeatStr(str, count) { return str.repeat(count); } - module.exports = repeatStr; - - - - - -// // Case: Manual inspection of the Error object -// // We use try/catch to log the error message to the console -// // to verify it's working as expected without breaking the test suite. -// test("Should log the specific error message to the console", () => { -// try { -// repeatStr("hi", -3); -// } catch (error) { -// console.log("Verified Error Message ->", error.message); - -// // Check if the message is exactly what we defined -// expect(error.message).toBe("count cannot be negative"); -// } - -// });