From e233decb614ec9132c83d77b793cdb4700b99579 Mon Sep 17 00:00:00 2001 From: Martin Mwaka Date: Mon, 29 Jun 2026 11:11:42 +0100 Subject: [PATCH 1/7] Write jest tests for 2-practice-tdd/count.test.js --- Sprint-3/2-practice-tdd/count.test.js | 37 +++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) diff --git a/Sprint-3/2-practice-tdd/count.test.js b/Sprint-3/2-practice-tdd/count.test.js index 179ea0ddf7..655f7b9ca1 100644 --- a/Sprint-3/2-practice-tdd/count.test.js +++ b/Sprint-3/2-practice-tdd/count.test.js @@ -22,3 +22,40 @@ 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 if no occurences of a character", () => { + const str = "fruit"; + const char = "p"; + const count = countChar(str, char); + expect(count).toBe(0); +}); + +// Scenario: Empty String +test("should return 0 if the string is empty", () => { + const str = ""; + const char = "a"; + const count = countChar(str, char); + expect(count).toBe(0); +}); + +// Scenario: Empty character +test("should return 0 if the character is empty", () => { + const str = "fruit"; + const char = ""; + const count = countChar(str, char); + expect(count).toBe(0); +}); + +// Scenario: Single character found +test("should return 1 if single occurrence of a character", () => { + const str = "fruit"; + const char = "r"; + const count = countChar(str, char); + expect(count).toBe(1); +}); + +test("should be case sensitive", () => { + const str = "coMmand"; + const char = "m"; + const count = countChar(str, char); + expect(count).toBe(1); +}); From 9e0a4cf13cbe8b36a062e1bb1f4aef91e3c97fcf Mon Sep 17 00:00:00 2001 From: Martin Mwaka Date: Mon, 29 Jun 2026 11:35:31 +0100 Subject: [PATCH 2/7] Complete countChar function to make all jest tests pass in 2-practice-tdd/count.js --- Sprint-3/2-practice-tdd/count.js | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/Sprint-3/2-practice-tdd/count.js b/Sprint-3/2-practice-tdd/count.js index 95b6ebb7d4..2fc399e2f8 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 + if (stringOfCharacters.length === 0 || findCharacter.length === 0) return 0; + let count = 0; + for (const char of stringOfCharacters) { + if (char === findCharacter) count++; + } + return count; } module.exports = countChar; From 8ca2290192991fe79df9996a09e71bd86ceffdfa Mon Sep 17 00:00:00 2001 From: Martin Mwaka Date: Tue, 30 Jun 2026 10:02:16 +0100 Subject: [PATCH 3/7] Write tests for get-ordinal-number.test.js --- .../2-practice-tdd/get-ordinal-number.test.js | 24 +++++++++++++++++++ 1 file changed, 24 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..e7b1273fe2 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,27 @@ 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(182)).toEqual("182nd"); +}); + +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(103)).toEqual("103rd"); +}); + +test("should append 'th' for numbers ending 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 not ending with 1, 2 or 3", () => { + expect(getOrdinalNumber(4)).toEqual("4th"); + expect(getOrdinalNumber(15)).toEqual("15th"); + expect(getOrdinalNumber(199)).toEqual("199th"); +}); From a2a025bed4a1f0f7cfbfa4bc97a4254dffdb02d8 Mon Sep 17 00:00:00 2001 From: Martin Mwaka Date: Tue, 30 Jun 2026 10:05:51 +0100 Subject: [PATCH 4/7] Complete function in get-ordinal-number.js --- 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..274ebb5330 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"; + let strNum; + const lastNum = num % 10; + const lastTwoNums = num % 100; + if (lastNum === 1 && lastTwoNums !== 11) { + strNum = `${String(num)}st`; + } else if (lastNum === 2 && lastTwoNums !== 12) { + strNum = `${String(num)}nd`; + } else if (lastNum === 3 && lastTwoNums !== 13) { + strNum = `${String(num)}rd`; + } else { + strNum = `${String(num)}th`; + } + return strNum; } module.exports = getOrdinalNumber; From d7441993519cecebb5600d1abe26834ba0cef5a5 Mon Sep 17 00:00:00 2001 From: Martin Mwaka Date: Tue, 30 Jun 2026 10:54:31 +0100 Subject: [PATCH 5/7] Write tests for 2-practice-tdd/repeat-str.test.js --- Sprint-3/2-practice-tdd/repeat-str.test.js | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/Sprint-3/2-practice-tdd/repeat-str.test.js b/Sprint-3/2-practice-tdd/repeat-str.test.js index a3fc1196c4..24eb4d373a 100644 --- a/Sprint-3/2-practice-tdd/repeat-str.test.js +++ b/Sprint-3/2-practice-tdd/repeat-str.test.js @@ -21,12 +21,28 @@ 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 if there is no repetition", () => { + 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 a negative number", () => { + const str = "hello"; + const count = -1; + expect(() => repeatStr(str, count)).toThrowError(); +}); From b9e71f02c8179c8b389c70ddc9d57d128cd066f9 Mon Sep 17 00:00:00 2001 From: Martin Mwaka Date: Tue, 30 Jun 2026 11:08:53 +0100 Subject: [PATCH 6/7] Write function for 2-practice-tdd/repeat.str.js --- Sprint-3/2-practice-tdd/repeat-str.js | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/Sprint-3/2-practice-tdd/repeat-str.js b/Sprint-3/2-practice-tdd/repeat-str.js index 2af0a2cea7..af24e2cb09 100644 --- a/Sprint-3/2-practice-tdd/repeat-str.js +++ b/Sprint-3/2-practice-tdd/repeat-str.js @@ -1,7 +1,16 @@ -function repeatStr() { +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("Number must be 0 or greater"); + } + + let repeatedStr = ""; + for (let i = 0; i < count; i++) { + repeatedStr += str; + } + return repeatedStr; } module.exports = repeatStr; From 1f99a76791eeaf0005df06e024b6bff92c671364 Mon Sep 17 00:00:00 2001 From: Martin Mwaka Date: Tue, 30 Jun 2026 11:17:46 +0100 Subject: [PATCH 7/7] Update function for 2-practice-tdd/repeat.str.js --- Sprint-3/2-practice-tdd/repeat-str.js | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/Sprint-3/2-practice-tdd/repeat-str.js b/Sprint-3/2-practice-tdd/repeat-str.js index af24e2cb09..38018fab48 100644 --- a/Sprint-3/2-practice-tdd/repeat-str.js +++ b/Sprint-3/2-practice-tdd/repeat-str.js @@ -6,11 +6,7 @@ function repeatStr(str, count) { throw new Error("Number must be 0 or greater"); } - let repeatedStr = ""; - for (let i = 0; i < count; i++) { - repeatedStr += str; - } - return repeatedStr; + return new Array(count).fill(str).join(""); } module.exports = repeatStr;