From c922c6950cf282deaea8735a9debbb00691643fc Mon Sep 17 00:00:00 2001 From: Francesco Romano Monda Date: Fri, 24 Jul 2026 16:26:14 +0100 Subject: [PATCH 1/3] Implement countChar, repeatStr, and getOrdinalNumber functions with TDD --- Sprint-3/2-practice-tdd/count.js | 10 ++++++++- Sprint-3/2-practice-tdd/count.test.js | 7 +++++++ Sprint-3/2-practice-tdd/get-ordinal-number.js | 18 +++++++++++++++- .../2-practice-tdd/get-ordinal-number.test.js | 12 +++++++++++ Sprint-3/2-practice-tdd/repeat-str.js | 14 ++++++++++--- Sprint-3/2-practice-tdd/repeat-str.test.js | 21 +++++++++++++++++++ 6 files changed, 77 insertions(+), 5 deletions(-) diff --git a/Sprint-3/2-practice-tdd/count.js b/Sprint-3/2-practice-tdd/count.js index 95b6ebb7d4..5407c3d7e3 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 char of stringOfCharacters) { + if (char === findCharacter) { + count++; + } + } + + return count; } module.exports = countChar; diff --git a/Sprint-3/2-practice-tdd/count.test.js b/Sprint-3/2-practice-tdd/count.test.js index 179ea0ddf7..372d158cc6 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 no occurrences of a character", () => { + const str = "aaaaa"; + const char = "b"; + const count = countChar(str, char); + expect(count).toEqual(0); +}); diff --git a/Sprint-3/2-practice-tdd/get-ordinal-number.js b/Sprint-3/2-practice-tdd/get-ordinal-number.js index f95d71db13..db8d4a8d24 100644 --- a/Sprint-3/2-practice-tdd/get-ordinal-number.js +++ b/Sprint-3/2-practice-tdd/get-ordinal-number.js @@ -1,5 +1,21 @@ function getOrdinalNumber(num) { - return "1st"; + if (num % 10 === 1) { + return `${num}st`; + } + + if (num % 10 === 2) { + return `${num}nd`; + } + + if (num % 10 === 3) { + return `${num}rd`; + } + + if (num % 100 === 11 || num % 100 === 12 || num % 100 === 13) { + return `${num}th`; + } + + return `${num}th`; } module.exports = getOrdinalNumber; 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..84e9847072 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,15 @@ 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(102)).toEqual("102nd"); +}); + +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"); +}); diff --git a/Sprint-3/2-practice-tdd/repeat-str.js b/Sprint-3/2-practice-tdd/repeat-str.js index 2af0a2cea7..69bd92d783 100644 --- a/Sprint-3/2-practice-tdd/repeat-str.js +++ b/Sprint-3/2-practice-tdd/repeat-str.js @@ -1,7 +1,15 @@ 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"; + if (count < 0) { + throw new Error("invalid"); + } + + let result = ""; + + for (let i = 0; i < count; i++) { + result += str; + } + + return result; } 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 a3fc1196c4..34e0b39f5b 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 repeat the string 1 time", () => { + 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 not repeat the string", () => { + 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 for negative count", () => { + const str = "hello"; + const count = -1; + const repeatedStr = repeatStr(str, count); + expect(() => repeatStr(str, count)).toThrow(); +}); From fa39a565f9b3edb1b218cbe3bb860a55f3d66370 Mon Sep 17 00:00:00 2001 From: Francesco Romano Monda Date: Mon, 27 Jul 2026 15:42:09 +0100 Subject: [PATCH 2/3] Fix repeatStr function parameters --- Sprint-3/2-practice-tdd/repeat-str.js | 2 +- package.json | 6 +++--- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/Sprint-3/2-practice-tdd/repeat-str.js b/Sprint-3/2-practice-tdd/repeat-str.js index 69bd92d783..8f10ba1617 100644 --- a/Sprint-3/2-practice-tdd/repeat-str.js +++ b/Sprint-3/2-practice-tdd/repeat-str.js @@ -1,4 +1,4 @@ -function repeatStr() { +function repeatStr(str, count) { if (count < 0) { throw new Error("invalid"); } diff --git a/package.json b/package.json index 0657e22dd8..5ebcdf7334 100644 --- a/package.json +++ b/package.json @@ -9,7 +9,7 @@ "keywords": [], "author": "Code Your Future", "license": "ISC", - "dependencies": { - "jest": "^29.7.0" + "devDependencies": { + "jest": "^30.4.2" } -} \ No newline at end of file +} From d867e6206a09c1e1d3eb6ac6720118685f6dae60 Mon Sep 17 00:00:00 2001 From: Francesco Romano Monda Date: Mon, 27 Jul 2026 16:02:31 +0100 Subject: [PATCH 3/3] Remove unintended package.json changes --- package.json | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/package.json b/package.json index 5ebcdf7334..0657e22dd8 100644 --- a/package.json +++ b/package.json @@ -9,7 +9,7 @@ "keywords": [], "author": "Code Your Future", "license": "ISC", - "devDependencies": { - "jest": "^30.4.2" + "dependencies": { + "jest": "^29.7.0" } -} +} \ No newline at end of file