From 860057a4b8f44cd6266a53673ba3e09b4ebb66d9 Mon Sep 17 00:00:00 2001 From: pathywang Date: Sun, 1 Mar 2026 20:11:49 +0000 Subject: [PATCH 1/5] commit --- Sprint-3/2-practice-tdd/count.js | 9 ++++++-- Sprint-3/2-practice-tdd/count.test.js | 23 +++++++------------ Sprint-3/2-practice-tdd/get-ordinal-number.js | 17 +++++++++++++- 3 files changed, 31 insertions(+), 18 deletions(-) diff --git a/Sprint-3/2-practice-tdd/count.js b/Sprint-3/2-practice-tdd/count.js index 95b6ebb7d..7b4d2ef51 100644 --- a/Sprint-3/2-practice-tdd/count.js +++ b/Sprint-3/2-practice-tdd/count.js @@ -1,5 +1,10 @@ function countChar(stringOfCharacters, findCharacter) { - return 5 -} + let count = 0 + for(let i=0;i { + expect(countChar("tired", "d")).toEqual(1) +}) -// 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"; - const count = countChar(str, char); - expect(count).toEqual(5); -}); - -// Scenario: No Occurrences +// Scenario: Multiple 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 no occurrences of a character", () => { + expect(countChar("bananataste","w")).toEqual(0) +}) \ No newline at end of file diff --git a/Sprint-3/2-practice-tdd/get-ordinal-number.js b/Sprint-3/2-practice-tdd/get-ordinal-number.js index f95d71db1..7a2c1f700 100644 --- a/Sprint-3/2-practice-tdd/get-ordinal-number.js +++ b/Sprint-3/2-practice-tdd/get-ordinal-number.js @@ -1,5 +1,20 @@ function getOrdinalNumber(num) { - return "1st"; + let lastDigit = num % 10; + let lastTwoDigits = num % 100; + if (lastTwoDigits >= 11 && lastTwoDigits <= 13) { + return num + "th"; + } + + switch (lastDigit) { + case 1: + return num + "st"; + case 2: + return num + "nd"; + case 3: + return num + "rd"; + default: + return num + "th"; + } } module.exports = getOrdinalNumber; From bd5993b6076c778d6aa3ca88df9cb8525aad31ea Mon Sep 17 00:00:00 2001 From: pathywang Date: Sun, 1 Mar 2026 20:14:52 +0000 Subject: [PATCH 2/5] commit --- Sprint-3/2-practice-tdd/count.test.js | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/Sprint-3/2-practice-tdd/count.test.js b/Sprint-3/2-practice-tdd/count.test.js index 07f8f44cf..c29e334b2 100644 --- a/Sprint-3/2-practice-tdd/count.test.js +++ b/Sprint-3/2-practice-tdd/count.test.js @@ -1,3 +1,4 @@ +// implement a function countChar that counts the number of times a character occurs in a string const countChar = require("./count"); // Given a string `str` and a single character `char` to search for, // When the countChar function is called with these inputs, @@ -9,6 +10,19 @@ test("should count occurrences of a character", () => { // 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"; + const count = countChar(str, char); + 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. From 41c14012e56afbafa17263df00940af1e16cdd57 Mon Sep 17 00:00:00 2001 From: pathywang Date: Sun, 1 Mar 2026 20:17:37 +0000 Subject: [PATCH 3/5] commit --- .../2-practice-tdd/get-ordinal-number.test.js | 28 +++++++++++++++++++ 1 file changed, 28 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..c7e8954ea 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,31 @@ test("should append 'st' for numbers ending with 1, except those ending with 11" expect(getOrdinalNumber(21)).toEqual("21st"); expect(getOrdinalNumber(131)).toEqual("131st"); }); + +// case 2: last two digits ending with 11,12,13 +test("should append 'th' for numbers with last digits 11,12,13", () =>{ + expect(getOrdinalNumber(11)).toEqual("11th"); + expect(getOrdinalNumber(313)).toEqual("313th") + expect(getOrdinalNumber(2112)).toEqual("2112th") +}) + +// Case 3: Numbers ending with 2(but not 12) +test("should append 'nd' for numbers ending with 2, except those ending with 12", () => { + expect(getOrdinalNumber(2)).toEqual("2nd"); + expect(getOrdinalNumber(52)).toEqual("52nd"); + expect(getOrdinalNumber(232)).toEqual("232nd"); +}); + +// Case 4: Numbers ending with 3(but not 13) +test("should append 'rd' for numbers ending with 3, except those ending with 13", () => { + expect(getOrdinalNumber(3)).toEqual("3rd"); + expect(getOrdinalNumber(83)).toEqual("83rd"); + expect(getOrdinalNumber(463)).toEqual("463rd"); +}); + +// case 5: Numbers ending with 4-9 and 0 +test("should append 'th' for numbers with last 4-9 and 0", () =>{ + expect(getOrdinalNumber(8)).toEqual("8th"); + expect(getOrdinalNumber(3130)).toEqual("3130th") + expect(getOrdinalNumber(2119)).toEqual("2119th") +}) \ No newline at end of file From 0f15af06e8fd79876250d668be0e9c0dd9759a54 Mon Sep 17 00:00:00 2001 From: pathywang Date: Sun, 1 Mar 2026 20:19:12 +0000 Subject: [PATCH 4/5] commit --- Sprint-3/2-practice-tdd/repeat-str.js | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/Sprint-3/2-practice-tdd/repeat-str.js b/Sprint-3/2-practice-tdd/repeat-str.js index 3838c7b00..193f1f8bd 100644 --- a/Sprint-3/2-practice-tdd/repeat-str.js +++ b/Sprint-3/2-practice-tdd/repeat-str.js @@ -1,5 +1,9 @@ -function repeatStr() { - return "hellohellohello"; +function repeatStr(str, count) { + if (count < 0) { + throw new Error("Count cannot be negative"); + } + + return str.repeat(count); } module.exports = repeatStr; From 09b5f7d95e41a094b9aeb8d4945337d9c8f32069 Mon Sep 17 00:00:00 2001 From: pathywang Date: Sun, 1 Mar 2026 20:21:06 +0000 Subject: [PATCH 5/5] commit --- Sprint-3/2-practice-tdd/repeat-str.test.js | 10 ++++++++++ 1 file changed, 10 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..ad1c378d2 100644 --- a/Sprint-3/2-practice-tdd/repeat-str.test.js +++ b/Sprint-3/2-practice-tdd/repeat-str.test.js @@ -20,13 +20,23 @@ 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 repeat the string only once", () => { + expect(repeatStr("nihao",1)).toEqual("nihao"); +}); // 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 repeat the string 0", () => { + expect(repeatStr("newyear",0)).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", () => { + expect(() => repeatStr("celebration", -5)) + .toThrow("Count cannot be negative"); +}); \ No newline at end of file