From 1cc87c56037318a2ff7b2b632a044b6381564bb0 Mon Sep 17 00:00:00 2001 From: XiaoQuark Date: Tue, 24 Feb 2026 11:26:44 +0000 Subject: [PATCH 1/8] complete 2-practice-tdd/count task --- Sprint-3/2-practice-tdd/count.js | 6 +++++- Sprint-3/2-practice-tdd/count.test.js | 7 +++++++ 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/Sprint-3/2-practice-tdd/count.js b/Sprint-3/2-practice-tdd/count.js index 95b6ebb7d..f573960c6 100644 --- a/Sprint-3/2-practice-tdd/count.js +++ b/Sprint-3/2-practice-tdd/count.js @@ -1,5 +1,9 @@ function countChar(stringOfCharacters, findCharacter) { - return 5 + const charRegex = new RegExp(findCharacter, "g"); + console.log(charRegex, typeof charRegex, "regex"); + const charArr = [...stringOfCharacters.matchAll(charRegex)]; + console.log(charArr, "array"); + return charArr.length; } module.exports = countChar; diff --git a/Sprint-3/2-practice-tdd/count.test.js b/Sprint-3/2-practice-tdd/count.test.js index 179ea0ddf..ae7aea7e2 100644 --- a/Sprint-3/2-practice-tdd/count.test.js +++ b/Sprint-3/2-practice-tdd/count.test.js @@ -22,3 +22,10 @@ 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 count 0 occurrences of a character", () => { + const str = "dog"; + const char = "a"; + const count = countChar(str, char); + expect(count).toEqual(0); +}); From a0605d6fcc880bb6f55b04948d0e0f502c55d25d Mon Sep 17 00:00:00 2001 From: XiaoQuark Date: Tue, 24 Feb 2026 11:27:09 +0000 Subject: [PATCH 2/8] refactor 2-practice-tdd/count task --- Sprint-3/2-practice-tdd/count.js | 2 -- 1 file changed, 2 deletions(-) diff --git a/Sprint-3/2-practice-tdd/count.js b/Sprint-3/2-practice-tdd/count.js index f573960c6..0a3ec6461 100644 --- a/Sprint-3/2-practice-tdd/count.js +++ b/Sprint-3/2-practice-tdd/count.js @@ -1,8 +1,6 @@ function countChar(stringOfCharacters, findCharacter) { const charRegex = new RegExp(findCharacter, "g"); - console.log(charRegex, typeof charRegex, "regex"); const charArr = [...stringOfCharacters.matchAll(charRegex)]; - console.log(charArr, "array"); return charArr.length; } From 13e7832b633c80afd55e1af5f2e8d9c03bd33d24 Mon Sep 17 00:00:00 2001 From: XiaoQuark Date: Tue, 24 Feb 2026 11:38:56 +0000 Subject: [PATCH 3/8] write tests for 2-practice-tdd/get-ordinal-number --- .../2-practice-tdd/get-ordinal-number.test.js | 21 +++++++++++++++++++ 1 file changed, 21 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 adfa58560..a9c6a5225 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,24 @@ 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(42)).toEqual("42nd"); + expect(getOrdinalNumber(782)).toEqual("782nd"); +}); + +test("should append 'rd' for numbers ending with 3, except those ending with 13", () => { + expect(getOrdinalNumber(3)).toEqual("3rd"); + expect(getOrdinalNumber(93)).toEqual("93rd"); + expect(getOrdinalNumber(7253)).toEqual("7253rd"); +}); + +test("should append 'th' for numbers ending with numbers 0, 4-9 and numbers whose last two digits are 11, 12, or 13", () => { + expect(getOrdinalNumber(11)).toEqual("11th"); + expect(getOrdinalNumber(12)).toEqual("12th"); + expect(getOrdinalNumber(13)).toEqual("13th"); + expect(getOrdinalNumber(20)).toEqual("20th"); + expect(getOrdinalNumber(75)).toEqual("75th"); + expect(getOrdinalNumber(567)).toEqual("567th"); +}); From f07827f71514d3f5a514c810c2118ce6748c2447 Mon Sep 17 00:00:00 2001 From: XiaoQuark Date: Tue, 24 Feb 2026 12:22:06 +0000 Subject: [PATCH 4/8] write function getOrdinalNumber() --- Sprint-3/2-practice-tdd/get-ordinal-number.js | 9 ++++++++- 1 file changed, 8 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..2f77f51a4 100644 --- a/Sprint-3/2-practice-tdd/get-ordinal-number.js +++ b/Sprint-3/2-practice-tdd/get-ordinal-number.js @@ -1,5 +1,12 @@ function getOrdinalNumber(num) { - return "1st"; + const lastTwoDigits = num % 100; + if (lastTwoDigits === 11 || lastTwoDigits === 12 || lastTwoDigits === 13) + return `${num}th`; + const lastDigit = num % 10; + 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 d80460d679a34e8d103fbd187dabddae4ef9c8c2 Mon Sep 17 00:00:00 2001 From: XiaoQuark Date: Tue, 24 Feb 2026 12:23:48 +0000 Subject: [PATCH 5/8] refactor tests for clearer cases --- .../2-practice-tdd/get-ordinal-number.test.js | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 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 a9c6a5225..345d8f04f 100644 --- a/Sprint-3/2-practice-tdd/get-ordinal-number.test.js +++ b/Sprint-3/2-practice-tdd/get-ordinal-number.test.js @@ -31,11 +31,18 @@ test("should append 'rd' for numbers ending with 3, except those ending with 13" expect(getOrdinalNumber(7253)).toEqual("7253rd"); }); -test("should append 'th' for numbers ending with numbers 0, 4-9 and numbers whose last two digits are 11, 12, or 13", () => { - expect(getOrdinalNumber(11)).toEqual("11th"); - expect(getOrdinalNumber(12)).toEqual("12th"); - expect(getOrdinalNumber(13)).toEqual("13th"); +test("should append 'th' for numbers ending with numbers 0 or 4-9", () => { expect(getOrdinalNumber(20)).toEqual("20th"); expect(getOrdinalNumber(75)).toEqual("75th"); expect(getOrdinalNumber(567)).toEqual("567th"); + expect(getOrdinalNumber(2574)).toEqual("2574th"); +}); + +test("should append 'th' for numbers ending with 11, 12, or 13", () => { + expect(getOrdinalNumber(11)).toEqual("11th"); + expect(getOrdinalNumber(12)).toEqual("12th"); + expect(getOrdinalNumber(13)).toEqual("13th"); + expect(getOrdinalNumber(512)).toEqual("512th"); + expect(getOrdinalNumber(113)).toEqual("113th"); + expect(getOrdinalNumber(6411)).toEqual("6411th"); }); From 5282cbf2feea7b54efd9228e3cbccf017fe47838 Mon Sep 17 00:00:00 2001 From: XiaoQuark Date: Tue, 24 Feb 2026 15:47:13 +0000 Subject: [PATCH 6/8] write tests for repeat-str task --- Sprint-3/2-practice-tdd/repeat-str.test.js | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/Sprint-3/2-practice-tdd/repeat-str.test.js b/Sprint-3/2-practice-tdd/repeat-str.test.js index a3fc1196c..cd642fe77 100644 --- a/Sprint-3/2-practice-tdd/repeat-str.test.js +++ b/Sprint-3/2-practice-tdd/repeat-str.test.js @@ -21,12 +21,33 @@ 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 return the original string when count is 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 an empty string when count is 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 throw an error when count is negative", () => { + const str = "hello"; + const count = -6; + const repeatedStr = () => repeatStr(str, count); + expect(repeatedStr).toThrow(/negative/); +}); From d09a54ea3412391159e266b5624263865362c5d6 Mon Sep 17 00:00:00 2001 From: XiaoQuark Date: Tue, 24 Feb 2026 16:01:39 +0000 Subject: [PATCH 7/8] write function repeat-str() --- Sprint-3/2-practice-tdd/repeat-str.js | 5 +++-- Sprint-3/2-practice-tdd/repeat-str.test.js | 2 +- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/Sprint-3/2-practice-tdd/repeat-str.js b/Sprint-3/2-practice-tdd/repeat-str.js index 3838c7b00..2bb544ba1 100644 --- a/Sprint-3/2-practice-tdd/repeat-str.js +++ b/Sprint-3/2-practice-tdd/repeat-str.js @@ -1,5 +1,6 @@ -function repeatStr() { - return "hellohellohello"; +function repeatStr(str, count) { + if (count >= 0) return str.repeat(count); + else throw new Error("Invalid format: count cannot be"); } module.exports = repeatStr; diff --git a/Sprint-3/2-practice-tdd/repeat-str.test.js b/Sprint-3/2-practice-tdd/repeat-str.test.js index cd642fe77..2bcde4f2a 100644 --- a/Sprint-3/2-practice-tdd/repeat-str.test.js +++ b/Sprint-3/2-practice-tdd/repeat-str.test.js @@ -49,5 +49,5 @@ test("should throw an error when count is negative", () => { const str = "hello"; const count = -6; const repeatedStr = () => repeatStr(str, count); - expect(repeatedStr).toThrow(/negative/); + expect(repeatedStr).toThrow(/Invalid format/); }); From b9967fa52a90598d17fd4acf8cd9536c3b22de24 Mon Sep 17 00:00:00 2001 From: XiaoQuark Date: Tue, 24 Feb 2026 16:02:23 +0000 Subject: [PATCH 8/8] refactor function repeatStr() --- Sprint-3/2-practice-tdd/repeat-str.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Sprint-3/2-practice-tdd/repeat-str.js b/Sprint-3/2-practice-tdd/repeat-str.js index 2bb544ba1..0c2556d05 100644 --- a/Sprint-3/2-practice-tdd/repeat-str.js +++ b/Sprint-3/2-practice-tdd/repeat-str.js @@ -1,6 +1,6 @@ function repeatStr(str, count) { - if (count >= 0) return str.repeat(count); - else throw new Error("Invalid format: count cannot be"); + if (count < 0) throw new Error("Invalid format: count cannot be negative"); + return str.repeat(count); } module.exports = repeatStr;