From 4ed9d6546f6ea77295b6a97ef5beb13fabe1dd6f Mon Sep 17 00:00:00 2001 From: Gideon Defar Date: Fri, 31 Jul 2026 17:06:45 +0100 Subject: [PATCH 1/8] Add initial tests for countChar --- Sprint-3/2-practice-tdd/count.test.js | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/Sprint-3/2-practice-tdd/count.test.js b/Sprint-3/2-practice-tdd/count.test.js index 179ea0ddf7..2c3e73a617 100644 --- a/Sprint-3/2-practice-tdd/count.test.js +++ b/Sprint-3/2-practice-tdd/count.test.js @@ -1,5 +1,6 @@ // 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, // Then it should: @@ -22,3 +23,11 @@ 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 character does not occur", () => { + const str = "hello"; + const char = "z"; + + const count = countChar(str, char); + + expect(count).toEqual(0); +}); \ No newline at end of file From 320a19562518b8a20471cd9b5f04c28f3e42924a Mon Sep 17 00:00:00 2001 From: Gideon Defar Date: Fri, 31 Jul 2026 17:07:10 +0100 Subject: [PATCH 2/8] Implement countChar --- Sprint-3/2-practice-tdd/count.js | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/Sprint-3/2-practice-tdd/count.js b/Sprint-3/2-practice-tdd/count.js index 95b6ebb7d4..562d103764 100644 --- a/Sprint-3/2-practice-tdd/count.js +++ b/Sprint-3/2-practice-tdd/count.js @@ -1,5 +1,13 @@ function countChar(stringOfCharacters, findCharacter) { - return 5 + let count = 0; + + for (const character of stringOfCharacters) { + if (character === findCharacter) { + count++; + } + } + + return count; } -module.exports = countChar; +module.exports = countChar; \ No newline at end of file From 384140c93894c693a33ea61a92976848ca767574 Mon Sep 17 00:00:00 2001 From: Gideon Defar Date: Sat, 1 Aug 2026 08:22:30 +0100 Subject: [PATCH 3/8] Add tests for ordinal numbers ending in 1 --- .../2-practice-tdd/get-ordinal-number.test.js | 40 +++++++++++++++++++ 1 file changed, 40 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..2956f24a91 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,43 @@ test("should append 'st' for numbers ending with 1, except those ending with 11" expect(getOrdinalNumber(21)).toEqual("21st"); expect(getOrdinalNumber(131)).toEqual("131st"); }); + +// Numbers ending with 1, but not 11 +test("should append 'st' for numbers ending with 1, except those ending with 11", () => { + expect(getOrdinalNumber(1)).toEqual("1st"); + expect(getOrdinalNumber(21)).toEqual("21st"); + expect(getOrdinalNumber(131)).toEqual("131st"); +}); + +// 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(22)).toEqual("22nd"); + expect(getOrdinalNumber(132)).toEqual("132nd"); +}); + +// 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(23)).toEqual("23rd"); + expect(getOrdinalNumber(133)).toEqual("133rd"); +}); + +// Numbers ending with 11, 12, or 13 +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"); +}); + +// Other numbers +test("should append 'th' for other numbers", () => { + expect(getOrdinalNumber(4)).toEqual("4th"); + expect(getOrdinalNumber(10)).toEqual("10th"); + expect(getOrdinalNumber(20)).toEqual("20th"); + expect(getOrdinalNumber(25)).toEqual("25th"); + expect(getOrdinalNumber(100)).toEqual("100th"); +}); \ No newline at end of file From 50ab5de84607f5b5096f70cb9d8952fe3fabc523 Mon Sep 17 00:00:00 2001 From: Gideon Defar Date: Sat, 1 Aug 2026 08:23:05 +0100 Subject: [PATCH 4/8] Implement ordinal numbers ending in 1 --- Sprint-3/2-practice-tdd/get-ordinal-number.js | 24 +++++++++++++++++-- 1 file changed, 22 insertions(+), 2 deletions(-) diff --git a/Sprint-3/2-practice-tdd/get-ordinal-number.js b/Sprint-3/2-practice-tdd/get-ordinal-number.js index f95d71db13..7f14d4d119 100644 --- a/Sprint-3/2-practice-tdd/get-ordinal-number.js +++ b/Sprint-3/2-practice-tdd/get-ordinal-number.js @@ -1,5 +1,25 @@ 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`; } -module.exports = getOrdinalNumber; +module.exports = getOrdinalNumber; \ No newline at end of file From 34208189c817c77607e7a6d658764639512a5433 Mon Sep 17 00:00:00 2001 From: Gideon Defar Date: Sat, 1 Aug 2026 08:23:28 +0100 Subject: [PATCH 5/8] Add tests for repeatStr --- Sprint-3/2-practice-tdd/repeat-str.test.js | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/Sprint-3/2-practice-tdd/repeat-str.test.js b/Sprint-3/2-practice-tdd/repeat-str.test.js index a3fc1196c4..63be37cc0e 100644 --- a/Sprint-3/2-practice-tdd/repeat-str.test.js +++ b/Sprint-3/2-practice-tdd/repeat-str.test.js @@ -21,12 +21,27 @@ 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 a string once", () => { + expect(repeatStr("hello", 1)).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 repeated zero times", () => { + expect(repeatStr("hello", 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", () => { + const str = "hello"; + const count = -1; + + expect(() => repeatStr(str, count)).toThrow(); +}); \ No newline at end of file From ec5846fa281a981c1a6b4d354be7d7ab0889d4c6 Mon Sep 17 00:00:00 2001 From: Gideon Defar Date: Sat, 1 Aug 2026 08:24:06 +0100 Subject: [PATCH 6/8] Implement repeatStr without repeat method --- Sprint-3/2-practice-tdd/repeat-str.js | 19 +++++++++++++++---- 1 file changed, 15 insertions(+), 4 deletions(-) diff --git a/Sprint-3/2-practice-tdd/repeat-str.js b/Sprint-3/2-practice-tdd/repeat-str.js index 2af0a2cea7..effe7ee2f0 100644 --- a/Sprint-3/2-practice-tdd/repeat-str.js +++ b/Sprint-3/2-practice-tdd/repeat-str.js @@ -1,7 +1,18 @@ -function repeatStr() { - // 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). +// function repeatStr() { + // 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"; +// return "hellohellohello"; +// } + +// module.exports = repeatStr; +function repeatStr(string, times) { + let result = ""; + + for (let i = 0; i < times; i++) { + result += string; + } + + return result; } -module.exports = repeatStr; +module.exports = repeatStr; \ No newline at end of file From 0c03b73ce639ea7125b118035310da8b836dfb4c Mon Sep 17 00:00:00 2001 From: Gideon Defar Date: Sun, 2 Aug 2026 19:39:55 +0100 Subject: [PATCH 7/8] Handle negative repeat count --- Sprint-3/2-practice-tdd/repeat-str.js | 7 ++++--- 1 file 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 effe7ee2f0..c961ebf6d3 100644 --- a/Sprint-3/2-practice-tdd/repeat-str.js +++ b/Sprint-3/2-practice-tdd/repeat-str.js @@ -2,12 +2,13 @@ // 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"; -// } - -// module.exports = repeatStr; function repeatStr(string, times) { let result = ""; + if (times < 0) { + throw new Error("Count cannot be negative"); + } + for (let i = 0; i < times; i++) { result += string; } From 3217f61550a401228da09a53050052cede859d27 Mon Sep 17 00:00:00 2001 From: Gideon Defar Date: Sun, 2 Aug 2026 19:47:24 +0100 Subject: [PATCH 8/8] Make the last test to pass and throw an error when count is -ve --- Sprint-3/2-practice-tdd/repeat-str.test.js | 3 ++- 1 file changed, 2 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 63be37cc0e..df338244a0 100644 --- a/Sprint-3/2-practice-tdd/repeat-str.test.js +++ b/Sprint-3/2-practice-tdd/repeat-str.test.js @@ -44,4 +44,5 @@ test("should throw an error when count is negative", () => { const count = -1; expect(() => repeatStr(str, count)).toThrow(); -}); \ No newline at end of file +}); +