From f136a71666c83527d7f5b610465b8200ebfb0ca3 Mon Sep 17 00:00:00 2001 From: Mahir Shah Date: Tue, 13 Oct 2026 15:16:34 +0100 Subject: [PATCH 01/11] Write tests for count.js --- practice-tdd/count.test.js | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/practice-tdd/count.test.js b/practice-tdd/count.test.js index cb3aaae59..3b968b149 100644 --- a/practice-tdd/count.test.js +++ b/practice-tdd/count.test.js @@ -17,8 +17,28 @@ test("should count multiple occurrences of a character", () => { expect(count).toEqual(5); }); +test("should count multiple occurrences of a character", () => { + const str = "ababa"; + const char = "a"; + const count = countChar(str, char); + expect(count).toEqual(3); +}); + +test("should count multiple occurrences of a character", () => { + const str = "abbba"; + const char = "a"; + const count = countChar(str, char); + expect(count).toEqual(2); +}); + // 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 count 0 occurrences of a character", () => { + const str = "aaaaa"; + const char = "b"; + const count = countChar(str, char); + expect(count).toEqual(0); +}); From bc6f87b8386707ccc4d6b310214cd5cad2e478e8 Mon Sep 17 00:00:00 2001 From: Mahir Shah Date: Tue, 13 Oct 2026 15:17:13 +0100 Subject: [PATCH 02/11] Write tests for get-ordinal-number.js --- practice-tdd/get-ordinal-number.test.js | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/practice-tdd/get-ordinal-number.test.js b/practice-tdd/get-ordinal-number.test.js index e23174a18..7e7ee6da8 100644 --- a/practice-tdd/get-ordinal-number.test.js +++ b/practice-tdd/get-ordinal-number.test.js @@ -18,3 +18,25 @@ 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("should append 'nd' for numbers ending with 2, except those ending with 12", () => { + expect(getOrdinalNumber(2)).toEqual("2nd"); + expect(getOrdinalNumber(22)).toEqual("22nd"); + expect(getOrdinalNumber(132)).toEqual("132nd"); +}); + +test("should append 'rd' for numbers ending with 3, except those ending with 13", () => { + expect(getOrdinalNumber(3)).toEqual("3rd"); + expect(getOrdinalNumber(23)).toEqual("23rd"); + expect(getOrdinalNumber(133)).toEqual("133rd"); +}); + +test("should append 'th' for numbers ending not ending with 1,2,3", () => { + expect(getOrdinalNumber(4)).toEqual("4th"); + expect(getOrdinalNumber(10)).toEqual("10th"); + expect(getOrdinalNumber(11)).toEqual("11th"); + expect(getOrdinalNumber(12)).toEqual("12th"); + expect(getOrdinalNumber(13)).toEqual("13th"); + expect(getOrdinalNumber(166)).toEqual("166th"); + expect(getOrdinalNumber(238)).toEqual("238th"); +}); From 48dddd21ef1c30d146a7e82be9b095ce23d77e3e Mon Sep 17 00:00:00 2001 From: Mahir Shah Date: Tue, 13 Oct 2026 15:24:51 +0100 Subject: [PATCH 03/11] Write tests for repeat-str.test.js --- practice-tdd/repeat-str.test.js | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/practice-tdd/repeat-str.test.js b/practice-tdd/repeat-str.test.js index 8a7036102..cdfbd0de9 100644 --- a/practice-tdd/repeat-str.test.js +++ b/practice-tdd/repeat-str.test.js @@ -21,12 +21,31 @@ test("should repeat the string count times", () => { // When the repeatStr function is called with these inputs, // Then it should return the original `str` without repetition. +test("should repeat the string count times, count = 1", () => { + const str = "hello"; + const count = 1; + const repeatedStr = repeatStr(str, count); + expect(repeatedStr).toEqual("hello"); +}); + // 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 empty string, count = 0", () => { + const str = "hello"; + const count = 0; + const repeatedStr = repeatStr(str, count); + expect(repeatedStr).toEqual(""); +}); // 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 return error, count < 0", () => { + const str = "hello"; + const count = -5; + const repeatedStr = repeatStr(str, count); + expect(repeatedStr).toThrow('Error'); +}); \ No newline at end of file From c7cd558aaeb164804d533cc4f0eb93cfa72c31ad Mon Sep 17 00:00:00 2001 From: Mahir Shah Date: Tue, 13 Oct 2026 15:33:22 +0100 Subject: [PATCH 04/11] Edit test names --- practice-tdd/count.test.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/practice-tdd/count.test.js b/practice-tdd/count.test.js index 3b968b149..52a0a484d 100644 --- a/practice-tdd/count.test.js +++ b/practice-tdd/count.test.js @@ -17,14 +17,14 @@ test("should count multiple occurrences of a character", () => { expect(count).toEqual(5); }); -test("should count multiple occurrences of a character", () => { +test("should count multiple occurrences of a character when mixed with other characters", () => { const str = "ababa"; const char = "a"; const count = countChar(str, char); expect(count).toEqual(3); }); -test("should count multiple occurrences of a character", () => { +test("should count multiple occurrences of a character at the beginning and end of a string", () => { const str = "abbba"; const char = "a"; const count = countChar(str, char); From 5f56a021253b53fa2da3802ebb8cf65af71f74dd Mon Sep 17 00:00:00 2001 From: Mahir Shah Date: Tue, 13 Oct 2026 15:37:43 +0100 Subject: [PATCH 05/11] Implement countChar function --- practice-tdd/count.js | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/practice-tdd/count.js b/practice-tdd/count.js index 5518c5712..1793df225 100644 --- a/practice-tdd/count.js +++ b/practice-tdd/count.js @@ -1,3 +1,9 @@ export function countChar(stringOfCharacters, findCharacter) { - return 5 + let count = 0; + for (let i = 0; i < stringOfCharacters.length; i++) { + if (stringOfCharacters[i] === findCharacter) { + count += 1; + } + } + return count; } From c1ea0a71572c155e8f184e43edfc3eea2fcbcdb5 Mon Sep 17 00:00:00 2001 From: Mahir Shah Date: Tue, 13 Oct 2026 15:44:20 +0100 Subject: [PATCH 06/11] Edit test name --- practice-tdd/get-ordinal-number.test.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/practice-tdd/get-ordinal-number.test.js b/practice-tdd/get-ordinal-number.test.js index 7e7ee6da8..463d1d0e8 100644 --- a/practice-tdd/get-ordinal-number.test.js +++ b/practice-tdd/get-ordinal-number.test.js @@ -31,7 +31,7 @@ test("should append 'rd' for numbers ending with 3, except those ending with 13" expect(getOrdinalNumber(133)).toEqual("133rd"); }); -test("should append 'th' for numbers ending not ending with 1,2,3", () => { +test("should append 'th' for numbers ending not ending with 1,2,3, except those ending with 11, 12, 13", () => { expect(getOrdinalNumber(4)).toEqual("4th"); expect(getOrdinalNumber(10)).toEqual("10th"); expect(getOrdinalNumber(11)).toEqual("11th"); From 966373aea0f1bfbf5a7b3fb6d5b2898a2a0a5ea7 Mon Sep 17 00:00:00 2001 From: Mahir Shah Date: Tue, 13 Oct 2026 15:46:08 +0100 Subject: [PATCH 07/11] Add extra tests --- practice-tdd/get-ordinal-number.test.js | 3 +++ 1 file changed, 3 insertions(+) diff --git a/practice-tdd/get-ordinal-number.test.js b/practice-tdd/get-ordinal-number.test.js index 463d1d0e8..cca751703 100644 --- a/practice-tdd/get-ordinal-number.test.js +++ b/practice-tdd/get-ordinal-number.test.js @@ -39,4 +39,7 @@ test("should append 'th' for numbers ending not ending with 1,2,3, except those expect(getOrdinalNumber(13)).toEqual("13th"); expect(getOrdinalNumber(166)).toEqual("166th"); expect(getOrdinalNumber(238)).toEqual("238th"); + expect(getOrdinalNumber(211)).toEqual("211th"); + expect(getOrdinalNumber(312)).toEqual("312th"); + expect(getOrdinalNumber(413)).toEqual("413th"); }); From 6ad7b43f4c665de81c4f5341ad7de3b3732b3d27 Mon Sep 17 00:00:00 2001 From: Mahir Shah Date: Tue, 13 Oct 2026 15:56:04 +0100 Subject: [PATCH 08/11] Implement getOrdinalNumber function --- practice-tdd/get-ordinal-number.js | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/practice-tdd/get-ordinal-number.js b/practice-tdd/get-ordinal-number.js index 5c6291f5e..f8357d8f0 100644 --- a/practice-tdd/get-ordinal-number.js +++ b/practice-tdd/get-ordinal-number.js @@ -1,3 +1,19 @@ export function getOrdinalNumber(num) { - return "1st"; + const numString = num.toString(); + const lastDigit = numString.slice(-1); + const last2Digit = numString.slice(-2); + + if (lastDigit === "1" && last2Digit !== "11") { + return numString + "st"; + } + + if (lastDigit === "2" && last2Digit !== "12") { + return numString + "nd"; + } + + if (lastDigit === "3" && last2Digit !== "13") { + return numString + "rd"; + } + + return numString + "th"; } From ea114684c7e87e2d6d6a1d2d70d73fe9611bd939 Mon Sep 17 00:00:00 2001 From: Mahir Shah Date: Tue, 13 Oct 2026 16:16:53 +0100 Subject: [PATCH 09/11] Fix test for negative count --- practice-tdd/repeat-str.test.js | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/practice-tdd/repeat-str.test.js b/practice-tdd/repeat-str.test.js index cdfbd0de9..a06403edf 100644 --- a/practice-tdd/repeat-str.test.js +++ b/practice-tdd/repeat-str.test.js @@ -46,6 +46,5 @@ test("should return empty string, count = 0", () => { test("should return error, count < 0", () => { const str = "hello"; const count = -5; - const repeatedStr = repeatStr(str, count); - expect(repeatedStr).toThrow('Error'); + expect(() => repeatStr(str, count)).toThrow(/Error/); }); \ No newline at end of file From afa99b83d938e8ea33f87e6503e3c901c2dd3218 Mon Sep 17 00:00:00 2001 From: Mahir Shah Date: Tue, 13 Oct 2026 16:17:26 +0100 Subject: [PATCH 10/11] Implement repeatStr function --- practice-tdd/repeat-str.js | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/practice-tdd/repeat-str.js b/practice-tdd/repeat-str.js index af273f3b0..cc96eec7f 100644 --- a/practice-tdd/repeat-str.js +++ b/practice-tdd/repeat-str.js @@ -1,5 +1,14 @@ -export function repeatStr() { +export function repeatStr(str, count) { // 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"; + if (count < 0) { + throw new Error("Error - invalid count number"); + } + + let repeatedStr = ""; + for (let i = count; i > 0; i--) { + repeatedStr += str; + } + + return repeatedStr; } From ffc8cc56e1a773b3b91228e13672bcbfaa93821d Mon Sep 17 00:00:00 2001 From: Mahir Shah Date: Tue, 13 Oct 2026 16:21:54 +0100 Subject: [PATCH 11/11] Format fix --- practice-tdd/repeat-str.test.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/practice-tdd/repeat-str.test.js b/practice-tdd/repeat-str.test.js index a06403edf..9d975e3ea 100644 --- a/practice-tdd/repeat-str.test.js +++ b/practice-tdd/repeat-str.test.js @@ -47,4 +47,4 @@ test("should return error, count < 0", () => { const str = "hello"; const count = -5; expect(() => repeatStr(str, count)).toThrow(/Error/); -}); \ No newline at end of file +});