From 2d01d60d4c56e2ecb6236d1795ffb3f3d90854d8 Mon Sep 17 00:00:00 2001 From: Yonatan Teklemariam Date: Sat, 27 Jun 2026 12:50:26 +0100 Subject: [PATCH 1/7] written jest tests for all the three functions and output tests as expected --- Sprint-3/2-practice-tdd/count.test.js | 19 ++++++++++++ .../2-practice-tdd/get-ordinal-number.test.js | 29 +++++++++++++++++++ Sprint-3/2-practice-tdd/repeat-str.js | 2 +- Sprint-3/2-practice-tdd/repeat-str.test.js | 19 +++++++++++- 4 files changed, 67 insertions(+), 2 deletions(-) diff --git a/Sprint-3/2-practice-tdd/count.test.js b/Sprint-3/2-practice-tdd/count.test.js index 179ea0ddf7..1a5c294214 100644 --- a/Sprint-3/2-practice-tdd/count.test.js +++ b/Sprint-3/2-practice-tdd/count.test.js @@ -18,6 +18,25 @@ test("should count multiple occurrences of a character", () => { }); // Scenario: No Occurrences +test(`should return zero the character doesn't exist in the string`, () => { + const str = "bravo"; + const char = "u"; + const count = countChar(str, char); + expect(count).toEqual(0); +}); + +// Empty String +test(`should return zero when the string is empty`, () => { + expect(countChar("", "a")).toEqual(0); +}); + +// Scenario: Multiple Occurrences +test("should count multiple occurrences of characters (including mixed and case-sensitive)", () => { + expect(countChar("aaaaa", "a")).toEqual(5); // simple multiple + expect(countChar("1-2-3-4-5-", "-")).toEqual(5); // mixed characters + expect(countChar("AaAa", "A")).toEqual(2); // case sensitivity +}); + // Given the input string `str`, // And a character `char` that does not exist within `str`. // When the function is called with these inputs, 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..17b39984bc 100644 --- a/Sprint-3/2-practice-tdd/get-ordinal-number.test.js +++ b/Sprint-3/2-practice-tdd/get-ordinal-number.test.js @@ -13,8 +13,37 @@ const getOrdinalNumber = require("./get-ordinal-number"); // Case 1: Numbers ending with 1 (but not 11) // When the number ends with 1, except those ending with 11, // Then the function should return a string by appending "st" to the number. + 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"); }); + +// Case 2: 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(102)).toEqual("102"); +}); + +// Case 3: 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(33)).toEqual("33rd"); + expect(getOrdinalNumber(103)).toEqual("103rd"); +}); + +// Case 4: Numbers ending with 11, 12, or 13 --- 11,12,13,111,121,131, +test("should append 'th' for numbers ending with 11, 12, 13", () => { + expect(getOrdinalNumber(11)).toEqual("11th"); + expect(getOrdinalNumber(12)).toEqual("12th"); + expect(getOrdinalNumber(13)).toEqual("13th"); +}); + +// Case 5: Numbers ending with larger numbers of 111, 121, 131 +test("should append 'th' for numbers ending with 111, 121, 131", () => { + expect(getOrdinalNumber(111)).toEqual("111th"); + expect(getOrdinalNumber(121)).toEqual("121th"); + expect(getOrdinalNumber(131)).toEqual("131th"); +}); diff --git a/Sprint-3/2-practice-tdd/repeat-str.js b/Sprint-3/2-practice-tdd/repeat-str.js index 2af0a2cea7..923599a0c9 100644 --- a/Sprint-3/2-practice-tdd/repeat-str.js +++ b/Sprint-3/2-practice-tdd/repeat-str.js @@ -1,7 +1,7 @@ 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 String.repeat(count); } 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..e6e5058df6 100644 --- a/Sprint-3/2-practice-tdd/repeat-str.test.js +++ b/Sprint-3/2-practice-tdd/repeat-str.test.js @@ -11,22 +11,39 @@ const repeatStr = require("./repeat-str"); test("should repeat the string count times", () => { const str = "hello"; - const count = 3; + const count = -5; const repeatedStr = repeatStr(str, count); expect(repeatedStr).toEqual("hellohellohello"); }); // Case: handle count of 1: +test("should repeat the string count of 1", () => { + const str = "fella"; + const count = 1; + const repeatedStr = repeatStr(str, count); + expect(repeatedStr).toEqual("fella"); +}); + // 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. // Case: Handle count of 0: +test("should repeat the string count of 0", () => { + const str = "fella"; + const count = 0; + const repeatedStr = repeatStr(str, count); + expect(repeatedStr).toEqual(""); +}); + // 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. // Case: Handle negative count: +test(`should throw an error for negative count`, () => { + expect(() => repeatedStr("fella", -2)).toThrowError(); +}); // 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. From 091f62b921e99cfd6282e52fa41e27c3ba4123eb Mon Sep 17 00:00:00 2001 From: Yonatan Teklemariam Date: Sat, 4 Jul 2026 03:14:41 +0100 Subject: [PATCH 2/7] Fix function name from repeatedStr to repeatStr --- Sprint-3/2-practice-tdd/repeat-str.test.js | 2 +- 1 file changed, 1 insertion(+), 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 e6e5058df6..80db82fb87 100644 --- a/Sprint-3/2-practice-tdd/repeat-str.test.js +++ b/Sprint-3/2-practice-tdd/repeat-str.test.js @@ -42,7 +42,7 @@ test("should repeat the string count of 0", () => { // Case: Handle negative count: test(`should throw an error for negative count`, () => { - expect(() => repeatedStr("fella", -2)).toThrowError(); + expect(() => repeatStr("fella", -2)).toThrowError(); }); // Given a target string `str` and a negative integer `count`, // When the repeatStr function is called with these inputs, From 49f7123d26537ac046c36986cde5287c20b04603 Mon Sep 17 00:00:00 2001 From: Yonatan Teklemariam Date: Sat, 4 Jul 2026 03:16:53 +0100 Subject: [PATCH 3/7] Update test to repeat string three times --- Sprint-3/2-practice-tdd/repeat-str.test.js | 2 +- 1 file changed, 1 insertion(+), 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 80db82fb87..e86f6622d1 100644 --- a/Sprint-3/2-practice-tdd/repeat-str.test.js +++ b/Sprint-3/2-practice-tdd/repeat-str.test.js @@ -11,7 +11,7 @@ const repeatStr = require("./repeat-str"); test("should repeat the string count times", () => { const str = "hello"; - const count = -5; + const count = 3; const repeatedStr = repeatStr(str, count); expect(repeatedStr).toEqual("hellohellohello"); }); From 16e1c5658f13f577e106f4d43cb3a4224b1273ed Mon Sep 17 00:00:00 2001 From: Yonatan Teklemariam Date: Sat, 4 Jul 2026 12:46:27 +0100 Subject: [PATCH 4/7] fixed the tdd tests --- Sprint-3/2-practice-tdd/count.js | 20 +++++++++++++++++-- Sprint-3/2-practice-tdd/get-ordinal-number.js | 19 +++++++++++++++++- .../2-practice-tdd/get-ordinal-number.test.js | 6 +++--- 3 files changed, 39 insertions(+), 6 deletions(-) diff --git a/Sprint-3/2-practice-tdd/count.js b/Sprint-3/2-practice-tdd/count.js index 95b6ebb7d4..791077dcce 100644 --- a/Sprint-3/2-practice-tdd/count.js +++ b/Sprint-3/2-practice-tdd/count.js @@ -1,5 +1,21 @@ function countChar(stringOfCharacters, findCharacter) { - return 5 + if ( + typeof stringOfCharacters !== "string" || + typeof findCharacter !== "string" + ) { + return 0; + } + let count = 0; + for (let i = 0; i < stringOfCharacters.length; i++) { + if (stringOfCharacters[i] === findCharacter) { + count++; + } + } + return count; } - +console.log(countChar("aaaaa", "a")); // should return 5 +console.log(countChar("bravo", "u")); // should return 0 +console.log(countChar("", "a")); // should return 0 +console.log(countChar("1-2-3-4-5-", "-")); // should return 5 +console.log(countChar("AaAa", "A")); // should return 2 module.exports = countChar; diff --git a/Sprint-3/2-practice-tdd/get-ordinal-number.js b/Sprint-3/2-practice-tdd/get-ordinal-number.js index f95d71db13..9041218b72 100644 --- a/Sprint-3/2-practice-tdd/get-ordinal-number.js +++ b/Sprint-3/2-practice-tdd/get-ordinal-number.js @@ -1,5 +1,22 @@ function getOrdinalNumber(num) { - return "1st"; + if (typeof num !== "number" || !Number.isInteger(num) || num < 0) { + throw new Error("Input must be a non-negative integer"); + } + + const lastTwo = num % 100; + const lastDigit = num % 10; + + // Special cases: 11th, 12th, 13th + if (lastTwo === 11 || lastTwo === 12 || lastTwo === 13) { + return `${num}th`; + } + + // Normal cases + if (lastDigit === 1) return `${num}st`; + if (lastDigit === 2) return `${num}nd`; + if (lastDigit === 3) return `${num}rd`; + + 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 17b39984bc..9ad595d301 100644 --- a/Sprint-3/2-practice-tdd/get-ordinal-number.test.js +++ b/Sprint-3/2-practice-tdd/get-ordinal-number.test.js @@ -42,8 +42,8 @@ test("should append 'th' for numbers ending with 11, 12, 13", () => { }); // Case 5: Numbers ending with larger numbers of 111, 121, 131 -test("should append 'th' for numbers ending with 111, 121, 131", () => { +test("should append 'th' for numbers ending with 111, 112, 113", () => { expect(getOrdinalNumber(111)).toEqual("111th"); - expect(getOrdinalNumber(121)).toEqual("121th"); - expect(getOrdinalNumber(131)).toEqual("131th"); + expect(getOrdinalNumber(112)).toEqual("112th"); + expect(getOrdinalNumber(113)).toEqual("113th"); }); From 9b54862dc9468f7a147546878bc6a472fb394d1a Mon Sep 17 00:00:00 2001 From: Yonatan Teklemariam Date: Sat, 4 Jul 2026 12:47:55 +0100 Subject: [PATCH 5/7] fixed typo in get-ordinal-number file --- Sprint-3/2-practice-tdd/get-ordinal-number.test.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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 9ad595d301..63a382a562 100644 --- a/Sprint-3/2-practice-tdd/get-ordinal-number.test.js +++ b/Sprint-3/2-practice-tdd/get-ordinal-number.test.js @@ -24,7 +24,7 @@ test("should append 'st' for numbers ending with 1, except those ending with 11" 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("102"); + expect(getOrdinalNumber(102)).toEqual("102nd"); }); // Case 3: Numbers ending with 3 (but NOT 13) From ce6155550a04e4868df8ddbb06ca860c397c532a Mon Sep 17 00:00:00 2001 From: Yonatan Teklemariam Date: Sat, 4 Jul 2026 12:58:49 +0100 Subject: [PATCH 6/7] corrected errors in tests and commited the changes --- Sprint-3/2-practice-tdd/repeat-str.js | 20 ++++++++++++++++---- 1 file changed, 16 insertions(+), 4 deletions(-) diff --git a/Sprint-3/2-practice-tdd/repeat-str.js b/Sprint-3/2-practice-tdd/repeat-str.js index 923599a0c9..859abd1040 100644 --- a/Sprint-3/2-practice-tdd/repeat-str.js +++ b/Sprint-3/2-practice-tdd/repeat-str.js @@ -1,7 +1,19 @@ -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 String.repeat(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. +function repeatStr(str, count) { + if (typeof str !== "string" || typeof count !== "number" || count < 0) { + throw new Error( + "Invalid input: str must be a string and count must be a non-negative integer" + ); + } + + let result = ""; + + for (let i = 0; i < count; i++) { + result += str; + } + + return result; } module.exports = repeatStr; From 2002729011f01eb9521cae37b8263297dcd3bcd3 Mon Sep 17 00:00:00 2001 From: Yonatan Teklemariam Date: Sat, 4 Jul 2026 13:54:30 +0100 Subject: [PATCH 7/7] added more tests for repeat-str-tests --- Sprint-3/2-practice-tdd/repeat-str.js | 17 +++++++++++++---- Sprint-3/2-practice-tdd/repeat-str.test.js | 17 ++++++++++++++++- 2 files changed, 29 insertions(+), 5 deletions(-) diff --git a/Sprint-3/2-practice-tdd/repeat-str.js b/Sprint-3/2-practice-tdd/repeat-str.js index 859abd1040..ee105b1386 100644 --- a/Sprint-3/2-practice-tdd/repeat-str.js +++ b/Sprint-3/2-practice-tdd/repeat-str.js @@ -1,10 +1,11 @@ // 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. function repeatStr(str, count) { - if (typeof str !== "string" || typeof count !== "number" || count < 0) { - throw new Error( - "Invalid input: str must be a string and count must be a non-negative integer" - ); + if (typeof str !== "string") { + return ""; + } + if (count < 0) { + throw new Error("Invalid input: count must be a non-negative integer"); } let result = ""; @@ -15,5 +16,13 @@ function repeatStr(str, count) { return result; } +console.log(repeatStr("hello", 3)); // Output: "hellohellohello" +console.log(repeatStr("fella", 1)); // Output: "fella" +console.log(repeatStr("fella", 0)); // Output: "" +console.log(repeatStr("ab", 3)); // Output: "ababab" +console.log(repeatStr("a", 1000).length); // Output: 1000 +console.log(repeatStr("", 5)); // Output: "" +console.log(repeatStr([], 3)); // Output: "" +console.log(repeatStr(null, 3)); // Output: "" 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 e86f6622d1..82ef0f1bfb 100644 --- a/Sprint-3/2-practice-tdd/repeat-str.test.js +++ b/Sprint-3/2-practice-tdd/repeat-str.test.js @@ -42,8 +42,23 @@ test("should repeat the string count of 0", () => { // Case: Handle negative count: test(`should throw an error for negative count`, () => { - expect(() => repeatStr("fella", -2)).toThrowError(); + expect(() => repeatStr("fella", -2)).toThrowError( + "Invalid input: count must be a non-negative integer" + ); }); // 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 return empty string for non-string input", () => { + expect(repeatStr([], 3)).toEqual(""); + expect(repeatStr(null, 3)).toEqual(""); +}); +test("should handle large repeat counts", () => { + expect(repeatStr("a", 1000).length).toEqual(1000); +}); +test("should repeat a multi-character string multiple times", () => { + expect(repeatStr("ab", 3)).toEqual("ababab"); +}); +test("should return an empty string when repeating an empty string", () => { + expect(repeatStr("", 5)).toEqual(""); +});