From 8d018fdf93ea10f09da44e538434ac127509b908 Mon Sep 17 00:00:00 2001 From: Sandani Kannangara Date: Mon, 22 Jun 2026 02:42:00 +0100 Subject: [PATCH 1/3] Implement countChar and add Jest tests --- Sprint-3/2-practice-tdd/count.js | 7 ++++-- Sprint-3/2-practice-tdd/count.test.js | 32 ++++++++++++++++++++++++++- 2 files changed, 36 insertions(+), 3 deletions(-) diff --git a/Sprint-3/2-practice-tdd/count.js b/Sprint-3/2-practice-tdd/count.js index 95b6ebb7d4..ff2db9a542 100644 --- a/Sprint-3/2-practice-tdd/count.js +++ b/Sprint-3/2-practice-tdd/count.js @@ -1,5 +1,8 @@ function countChar(stringOfCharacters, findCharacter) { - return 5 + let count = 0; + for (const c of stringOfCharacters) { + if (c === 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..9f6ab14e78 100644 --- a/Sprint-3/2-practice-tdd/count.test.js +++ b/Sprint-3/2-practice-tdd/count.test.js @@ -10,11 +10,41 @@ const countChar = require("./count"); // When the function is called with these inputs, // Then it should correctly count occurrences of `char`. -test("should count multiple occurrences of a character", () => { +/*test("should count multiple occurrences of a character", () => { const str = "aaaaa"; const char = "a"; const count = countChar(str, char); expect(count).toEqual(5); +});*/ + +describe("countChar", () => { + test("counts repeated characters in a simple string", () => { + expect(countChar("aaaaa", "a")).toEqual(5); + }); + + test("counts characters in a mixed string", () => { + expect(countChar("absankama", "a")).toEqual(4); + }); + + test("counts characters including spaces", () => { + expect(countChar("ab san kama", "a")).toEqual(4); + }); + + test("counts characters in string with symbols", () => { + expect(countChar("a-+&£$%?/|b san kama", "a")).toEqual(4); + }); + + test("returns 0 when character is not found", () => { + expect(countChar("-+&£$%?/|b sn km", "a")).toEqual(0); + }); + + test("returns 0 for empty string", () => { + expect(countChar("", "a")).toEqual(0); + }); + + test("is case sensitive", () => { + expect(countChar("AaAa", "a")).toEqual(2); + }); }); // Scenario: No Occurrences From a156bac44778e831633ed0a8ed75f7aa85c16c7f Mon Sep 17 00:00:00 2001 From: Sandani Kannangara Date: Mon, 22 Jun 2026 03:15:10 +0100 Subject: [PATCH 2/3] Implement getOrdinalNumber and add Jest tests --- Sprint-3/2-practice-tdd/get-ordinal-number.js | 8 ++++- .../2-practice-tdd/get-ordinal-number.test.js | 30 ++++++++++++++++++- 2 files changed, 36 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..34226272f0 100644 --- a/Sprint-3/2-practice-tdd/get-ordinal-number.js +++ b/Sprint-3/2-practice-tdd/get-ordinal-number.js @@ -1,5 +1,11 @@ function getOrdinalNumber(num) { - return "1st"; + const endDigit = num % 10; + const endTwoDigits = num % 100; + if (endTwoDigits >= 11 && endTwoDigits <= 13) return num + "th"; + if (endDigit === 1) return num + "st"; + if (endDigit === 2) return num + "nd"; + if (endDigit === 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 adfa58560f..ccc6198acc 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,36 @@ 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", () => { +/*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"); +});*/ + +describe("getOrdinalNumber", () => { + test("appends th for numbers ending in 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"); + }); + test("appends st for numbers ending in 1 but not 11", () => { + expect(getOrdinalNumber(1)).toEqual("1st"); + expect(getOrdinalNumber(131)).toEqual("131st"); + }); + test("appends nd for numbers ending in 2 but not 12", () => { + expect(getOrdinalNumber(2)).toEqual("2nd"); + expect(getOrdinalNumber(932)).toEqual("932nd"); + }); + test("appends rd for numbers ending in 3 but not 13", () => { + expect(getOrdinalNumber(3)).toEqual("3rd"); + expect(getOrdinalNumber(33)).toEqual("33rd"); + }); + test("appends th for all other numbers", () => { + expect(getOrdinalNumber(20)).toEqual("20th"); + expect(getOrdinalNumber(24)).toEqual("24th"); + expect(getOrdinalNumber(100)).toEqual("100th"); + }); }); From 9f0252caaafb6ae9e6a397a4ce389f60e63258e3 Mon Sep 17 00:00:00 2001 From: Sandani Kannangara Date: Mon, 22 Jun 2026 14:09:25 +0100 Subject: [PATCH 3/3] Implement repeat-str and add Jest tests --- Sprint-3/2-practice-tdd/repeat-str.js | 10 +++++++-- Sprint-3/2-practice-tdd/repeat-str.test.js | 25 +++++++++++++++++----- 2 files changed, 28 insertions(+), 7 deletions(-) diff --git a/Sprint-3/2-practice-tdd/repeat-str.js b/Sprint-3/2-practice-tdd/repeat-str.js index 2af0a2cea7..b4dfe0e512 100644 --- a/Sprint-3/2-practice-tdd/repeat-str.js +++ b/Sprint-3/2-practice-tdd/repeat-str.js @@ -1,7 +1,13 @@ -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"; + let combinedStr = ""; + if (count < 0) throw new Error("Invalid Count"); + + for (let i = 0; i < count; i++) { + combinedStr += str; + } + return combinedStr; } 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..84dab0bb3e 100644 --- a/Sprint-3/2-practice-tdd/repeat-str.test.js +++ b/Sprint-3/2-practice-tdd/repeat-str.test.js @@ -9,11 +9,26 @@ const repeatStr = require("./repeat-str"); // When the repeatStr function is called with these inputs, // Then it should return a string that contains the original `str` repeated `count` times. -test("should repeat the string count times", () => { - const str = "hello"; - const count = 3; - const repeatedStr = repeatStr(str, count); - expect(repeatedStr).toEqual("hellohellohello"); +describe("repeatStr", () => { + test("repeats string count times when count is greater than 1", () => { + expect(repeatStr("hello", 3)).toEqual("hellohellohello"); + }); + + test("returns original string when count is 1", () => { + expect(repeatStr("hello", 1)).toEqual("hello"); + }); + + test("returns empty string when count is 0", () => { + expect(repeatStr("hello", 0)).toEqual(""); + }); + + test("throws an error when count is negative", () => { + expect(() => repeatStr("hello", -3)).toThrow("Invalid Count"); + }); + + test("returns empty string when the input string is empty", () => { + expect(repeatStr("", 3)).toEqual(""); + }); }); // Case: handle count of 1: