From 37f8d798e67545b2104bc742a6f1e0589089f1cf Mon Sep 17 00:00:00 2001 From: Brian Yorachi Date: Wed, 14 Oct 2026 15:23:21 +0100 Subject: [PATCH 1/4] TDD countChar: handle no-occurrence case --- practice-tdd/count.js | 10 +++++++++- practice-tdd/count.test.js | 7 +++++++ 2 files changed, 16 insertions(+), 1 deletion(-) diff --git a/practice-tdd/count.js b/practice-tdd/count.js index 5518c5712..fc22f6db8 100644 --- a/practice-tdd/count.js +++ b/practice-tdd/count.js @@ -1,3 +1,11 @@ export function countChar(stringOfCharacters, findCharacter) { - return 5 + let count = 0; + + for (const character of stringOfCharacters) { + if (character === findCharacter) { + count += 1; + } + } + + return count; } diff --git a/practice-tdd/count.test.js b/practice-tdd/count.test.js index cb3aaae59..81723d894 100644 --- a/practice-tdd/count.test.js +++ b/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 return 0 when the character does not occur", () => { + const str = "hello"; + const char = "z"; + const count = countChar(str, char); + expect(count).toEqual(0); +}); From de8c4b0e93fd965bec21bf0f8612b2a2ef28de29 Mon Sep 17 00:00:00 2001 From: Brian Yorachi Date: Wed, 14 Oct 2026 15:29:13 +0100 Subject: [PATCH 2/4] TDD repeatStr: handle count 1, 0, and negative cases --- practice-tdd/repeat-str.js | 14 ++++++++++++-- practice-tdd/repeat-str.test.js | 20 ++++++++++++++++++++ 2 files changed, 32 insertions(+), 2 deletions(-) diff --git a/practice-tdd/repeat-str.js b/practice-tdd/repeat-str.js index af273f3b0..2881d5e18 100644 --- a/practice-tdd/repeat-str.js +++ b/practice-tdd/repeat-str.js @@ -1,5 +1,15 @@ -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("count must not be negative"); + } + + let result = ""; + + for (let i = 0; i < count; i += 1) { + result += str; + } + + return result; } diff --git a/practice-tdd/repeat-str.test.js b/practice-tdd/repeat-str.test.js index 8a7036102..8f43bca38 100644 --- a/practice-tdd/repeat-str.test.js +++ b/practice-tdd/repeat-str.test.js @@ -21,12 +21,32 @@ 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 = -1; + expect(() => repeatStr(str, count)).toThrow(); +}); From 26e6bc90467f3751ed1ce2f061cc2444b723e7fd Mon Sep 17 00:00:00 2001 From: Brian Yorachi Date: Wed, 14 Oct 2026 15:35:14 +0100 Subject: [PATCH 3/4] TDD getOrdinalNumber: handle nd, rd, th, and teen exceptions --- practice-tdd/get-ordinal-number.js | 22 ++++++++++++- practice-tdd/get-ordinal-number.test.js | 41 +++++++++++++++++++++++++ 2 files changed, 62 insertions(+), 1 deletion(-) diff --git a/practice-tdd/get-ordinal-number.js b/practice-tdd/get-ordinal-number.js index 5c6291f5e..81dbae083 100644 --- a/practice-tdd/get-ordinal-number.js +++ b/practice-tdd/get-ordinal-number.js @@ -1,3 +1,23 @@ export function getOrdinalNumber(num) { - return "1st"; + const lastTwoDigits = num % 100; + + if (lastTwoDigits >= 11 && 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`; } diff --git a/practice-tdd/get-ordinal-number.test.js b/practice-tdd/get-ordinal-number.test.js index e23174a18..18261d226 100644 --- a/practice-tdd/get-ordinal-number.test.js +++ b/practice-tdd/get-ordinal-number.test.js @@ -18,3 +18,44 @@ 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: Numbers ending with 2 (but not 12) +// When the number ends with 2, except those ending with 12, +// Then the function should return a string by appending "nd" to the number. +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"); +}); + +// Case 3: Numbers ending with 3 (but not 13) +// When the number ends with 3, except those ending with 13, +// Then the function should return a string by appending "rd" to the number. +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"); +}); + +// Case 4: Numbers ending with 11, 12, or 13 +// When the number ends with 11, 12, or 13, +// Then the function should return a string by appending "th" to the number, +// regardless of the rules for 1, 2, or 3. +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(111)).toEqual("111th"); + expect(getOrdinalNumber(112)).toEqual("112th"); + expect(getOrdinalNumber(113)).toEqual("113th"); +}); + +// Case 5: Numbers ending with 0 or 4-9 +// When the number ends with any digit other than 1, 2, or 3 (and is not 11, 12, or 13), +// Then the function should return a string by appending "th" to the number. +test("should append 'th' for numbers ending with 0 or 4-9", () => { + expect(getOrdinalNumber(4)).toEqual("4th"); + expect(getOrdinalNumber(10)).toEqual("10th"); + expect(getOrdinalNumber(100)).toEqual("100th"); + expect(getOrdinalNumber(109)).toEqual("109th"); +}); From 8a70a6da2b5d09cd5697e72e217944ffcebd78b3 Mon Sep 17 00:00:00 2001 From: Brian Yorachi Date: Mon, 19 Oct 2026 17:47:27 +0100 Subject: [PATCH 4/4] Add empty-string and partial-match tests --- practice-tdd/count.test.js | 14 ++++++++++++++ practice-tdd/repeat-str.test.js | 4 ++++ 2 files changed, 18 insertions(+) diff --git a/practice-tdd/count.test.js b/practice-tdd/count.test.js index 81723d894..a89123c4b 100644 --- a/practice-tdd/count.test.js +++ b/practice-tdd/count.test.js @@ -17,6 +17,13 @@ test("should count multiple occurrences of a character", () => { expect(count).toEqual(5); }); +test("should count occurrences when the character appears within the string", () => { + const str = "hello"; + const char = "l"; + 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`. @@ -29,3 +36,10 @@ test("should return 0 when the character does not occur", () => { const count = countChar(str, char); expect(count).toEqual(0); }); + +test("should return 0 when the string is empty", () => { + const str = ""; + const char = "z"; + const count = countChar(str, char); + expect(count).toEqual(0); +}); diff --git a/practice-tdd/repeat-str.test.js b/practice-tdd/repeat-str.test.js index 8f43bca38..f42ee2a43 100644 --- a/practice-tdd/repeat-str.test.js +++ b/practice-tdd/repeat-str.test.js @@ -14,6 +14,7 @@ test("should repeat the string count times", () => { const count = 3; const repeatedStr = repeatStr(str, count); expect(repeatedStr).toEqual("hellohellohello"); + expect(repeatStr("", count)).toEqual(""); }); // Case: handle count of 1: @@ -26,6 +27,7 @@ test("should return the original string when count is 1", () => { const count = 1; const repeatedStr = repeatStr(str, count); expect(repeatedStr).toEqual("hello"); + expect(repeatStr("", count)).toEqual(""); }); // Case: Handle count of 0: @@ -38,6 +40,7 @@ test("should return an empty string when count is 0", () => { const count = 0; const repeatedStr = repeatStr(str, count); expect(repeatedStr).toEqual(""); + expect(repeatStr("", count)).toEqual(""); }); // Case: Handle negative count: @@ -49,4 +52,5 @@ test("should throw an error when count is negative", () => { const str = "hello"; const count = -1; expect(() => repeatStr(str, count)).toThrow(); + expect(() => repeatStr("", count)).toThrow(); });