From 59f9fe219d8797267df09052a4236a1460f9db19 Mon Sep 17 00:00:00 2001 From: cywong Date: Fri, 10 Jul 2026 18:25:28 +0100 Subject: [PATCH 1/7] Implement character counting in countChar function --- Sprint-3/2-practice-tdd/count.js | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/Sprint-3/2-practice-tdd/count.js b/Sprint-3/2-practice-tdd/count.js index 95b6ebb7d4..683a39bf56 100644 --- a/Sprint-3/2-practice-tdd/count.js +++ b/Sprint-3/2-practice-tdd/count.js @@ -1,5 +1,12 @@ function countChar(stringOfCharacters, findCharacter) { - return 5 + let count = 0; + let char = ""; + for (let char of stringOfCharacters) { + if (char === findCharacter) { + count++; + } + } + return count; } module.exports = countChar; From 3ea88d9f7f6c2187882998240b44c316a2db8745 Mon Sep 17 00:00:00 2001 From: cywong Date: Fri, 10 Jul 2026 18:26:19 +0100 Subject: [PATCH 2/7] Add test for empty string in countChar function --- Sprint-3/2-practice-tdd/count.test.js | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/Sprint-3/2-practice-tdd/count.test.js b/Sprint-3/2-practice-tdd/count.test.js index 179ea0ddf7..29b749d31f 100644 --- a/Sprint-3/2-practice-tdd/count.test.js +++ b/Sprint-3/2-practice-tdd/count.test.js @@ -22,3 +22,9 @@ test("should count multiple occurrences of a character", () => { // 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 0 for an empty string", () => { + const str = ""; + const char = "a"; + const count = countChar(str, char); + expect(count).toEqual(0); +}); From 6f56e8d4ac9bf62c8f599cd0ccb795e5a16e873a Mon Sep 17 00:00:00 2001 From: cywong Date: Fri, 10 Jul 2026 18:26:59 +0100 Subject: [PATCH 3/7] Add ordinal number logic to getOrdinalNumber function Implement logic to return ordinal numbers based on input. --- Sprint-3/2-practice-tdd/get-ordinal-number.js | 14 +++++++++++++- 1 file changed, 13 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..3d8606ed6e 100644 --- a/Sprint-3/2-practice-tdd/get-ordinal-number.js +++ b/Sprint-3/2-practice-tdd/get-ordinal-number.js @@ -1,5 +1,17 @@ function getOrdinalNumber(num) { - return "1st"; + const lastTwoDigits = num % 100; + + // Handle special cases: 11th, 12th, 13th + if (lastTwoDigits >= 11 && lastTwoDigits <= 13) { + return `${num}th`; + } + + // Normal rules + 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 a7e8edd9f0cdd9ff6aa7f37731809f2f34ff1e3c Mon Sep 17 00:00:00 2001 From: cywong Date: Fri, 10 Jul 2026 18:27:42 +0100 Subject: [PATCH 4/7] Add tests for ordinal number function Added tests for ordinal number handling for 2nd, 3rd, th exceptions, and general cases. --- .../2-practice-tdd/get-ordinal-number.test.js | 25 +++++++++++++++++++ 1 file changed, 25 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..96c383011d 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,28 @@ test("should append 'st' for numbers ending with 1, except those ending with 11" expect(getOrdinalNumber(21)).toEqual("21st"); expect(getOrdinalNumber(131)).toEqual("131st"); }); + +test("handles numbers ending in 2 (nd)", () => { + expect(getOrdinalNumber(2)).toBe("2nd"); + expect(getOrdinalNumber(22)).toBe("22nd"); + expect(getOrdinalNumber(102)).toBe("102nd"); +}); + +test("handles numbers ending in 3 (rd)", () => { + expect(getOrdinalNumber(3)).toBe("3rd"); + expect(getOrdinalNumber(23)).toBe("23rd"); + expect(getOrdinalNumber(103)).toBe("103rd"); +}); + +test("handles numbers ending in 11, 12, 13 (th exceptions)", () => { + expect(getOrdinalNumber(11)).toBe("11th"); + expect(getOrdinalNumber(12)).toBe("12th"); + expect(getOrdinalNumber(13)).toBe("13th"); + expect(getOrdinalNumber(111)).toBe("111th"); +}); + +test("handles general th cases", () => { + expect(getOrdinalNumber(4)).toBe("4th"); + expect(getOrdinalNumber(20)).toBe("20th"); + expect(getOrdinalNumber(100)).toBe("100th"); +}); From e28b498ee838591158d2f9d574ca3449febb3255 Mon Sep 17 00:00:00 2001 From: cywong Date: Fri, 10 Jul 2026 18:28:15 +0100 Subject: [PATCH 5/7] Implement repeatStr function with error handling Implement repeatStr function to repeat a string a given number of times without using String.prototype.repeat. Handle negative count by throwing an error. --- Sprint-3/2-practice-tdd/repeat-str.js | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/Sprint-3/2-practice-tdd/repeat-str.js b/Sprint-3/2-practice-tdd/repeat-str.js index 2af0a2cea7..d629fe9ade 100644 --- a/Sprint-3/2-practice-tdd/repeat-str.js +++ b/Sprint-3/2-practice-tdd/repeat-str.js @@ -1,7 +1,17 @@ function repeatStr() { // Your implementation of this function must *not* call String.prototype.repeat (https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/repeat). // The goal is to re-implement that function, not to use it. - return "hellohellohello"; + + for (let i = 0; i < count; i++) { + result += str; + } + + if (count >= 0) { + return result; + } + else { + throw new Error("Count cannot be negative"); + } } module.exports = repeatStr; From 61e294b3cca8dde898b49a628be52d80b91b90ff Mon Sep 17 00:00:00 2001 From: cywong Date: Fri, 10 Jul 2026 18:30:50 +0100 Subject: [PATCH 6/7] Add edge case tests for repeatStr function Added test cases for repeatStr function to handle edge cases including count of 1, 0, and negative values. --- Sprint-3/2-practice-tdd/repeat-str.test.js | 17 +++++++++++++++++ 1 file changed, 17 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..13e1916f3a 100644 --- a/Sprint-3/2-practice-tdd/repeat-str.test.js +++ b/Sprint-3/2-practice-tdd/repeat-str.test.js @@ -16,6 +16,23 @@ test("should repeat the string count times", () => { expect(repeatedStr).toEqual("hellohellohello"); }); +// Case: handle count of 1 +test("should repeat the string 1 time", () => { + const result = repeatStr("hello", 1); + expect(result).toEqual("hello"); +}); + +// Case: handle count of 0 +test("should return an empty string when count is 0", () => { + const result = repeatStr("hello", 0); + expect(result).toEqual(""); +}); + +// Case: handle negative count +test("should throw an error when count is negative", () => { + expect(() => repeatStr("hello", -3)).toThrow(); +}); + // Case: handle count of 1: // Given a target string `str` and a `count` equal to 1, // When the repeatStr function is called with these inputs, From c1996f95dbe02902c3929749569b526437466e3b Mon Sep 17 00:00:00 2001 From: cywong Date: Fri, 10 Jul 2026 18:32:10 +0100 Subject: [PATCH 7/7] Update test assertions to use toEqual --- .../2-practice-tdd/get-ordinal-number.test.js | 26 +++++++++---------- 1 file changed, 13 insertions(+), 13 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 96c383011d..da2927b687 100644 --- a/Sprint-3/2-practice-tdd/get-ordinal-number.test.js +++ b/Sprint-3/2-practice-tdd/get-ordinal-number.test.js @@ -20,26 +20,26 @@ test("should append 'st' for numbers ending with 1, except those ending with 11" }); test("handles numbers ending in 2 (nd)", () => { - expect(getOrdinalNumber(2)).toBe("2nd"); - expect(getOrdinalNumber(22)).toBe("22nd"); - expect(getOrdinalNumber(102)).toBe("102nd"); + expect(getOrdinalNumber(2)).toEqual("2nd"); + expect(getOrdinalNumber(22)).toEqual("22nd"); + expect(getOrdinalNumber(102)).toEqual("102nd"); }); test("handles numbers ending in 3 (rd)", () => { - expect(getOrdinalNumber(3)).toBe("3rd"); - expect(getOrdinalNumber(23)).toBe("23rd"); - expect(getOrdinalNumber(103)).toBe("103rd"); + expect(getOrdinalNumber(3)).toEqual("3rd"); + expect(getOrdinalNumber(23)).toEqual("23rd"); + expect(getOrdinalNumber(103)).toEqual("103rd"); }); test("handles numbers ending in 11, 12, 13 (th exceptions)", () => { - expect(getOrdinalNumber(11)).toBe("11th"); - expect(getOrdinalNumber(12)).toBe("12th"); - expect(getOrdinalNumber(13)).toBe("13th"); - expect(getOrdinalNumber(111)).toBe("111th"); + expect(getOrdinalNumber(11)).toEqual("11th"); + expect(getOrdinalNumber(12)).toEqual("12th"); + expect(getOrdinalNumber(13)).toEqual("13th"); + expect(getOrdinalNumber(111)).toEqual("111th"); }); test("handles general th cases", () => { - expect(getOrdinalNumber(4)).toBe("4th"); - expect(getOrdinalNumber(20)).toBe("20th"); - expect(getOrdinalNumber(100)).toBe("100th"); + expect(getOrdinalNumber(4)).toEqual("4th"); + expect(getOrdinalNumber(20)).toEqual("20th"); + expect(getOrdinalNumber(100)).toEqual("100th"); });