From a69ca5da55b3c2d7ff00f40c90ed9d7997506965 Mon Sep 17 00:00:00 2001 From: tasleemadedokun Date: Fri, 6 Mar 2026 12:54:53 +0000 Subject: [PATCH 1/3] Effect Changes to the count.js and count.test.js --- Sprint-3/2-practice-tdd/count.js | 12 +++++++++++- Sprint-3/2-practice-tdd/count.test.js | 8 ++++++++ 2 files changed, 19 insertions(+), 1 deletion(-) diff --git a/Sprint-3/2-practice-tdd/count.js b/Sprint-3/2-practice-tdd/count.js index 95b6ebb7d4..1ab61aae35 100644 --- a/Sprint-3/2-practice-tdd/count.js +++ b/Sprint-3/2-practice-tdd/count.js @@ -1,5 +1,15 @@ function countChar(stringOfCharacters, findCharacter) { - return 5 + let count = 0; + + for (let i = 0; i < stringOfCharacters.length; i++) { + if (stringOfCharacters[i] === findCharacter) { + count++; + } + } + + return count; } module.exports = countChar; +console.log(countChar("aaaaa", "a"));//print 5 +console.log(countChar("aaaaa", "z"));//print 0 \ No newline at end of file diff --git a/Sprint-3/2-practice-tdd/count.test.js b/Sprint-3/2-practice-tdd/count.test.js index 179ea0ddf7..2623a6cd9b 100644 --- a/Sprint-3/2-practice-tdd/count.test.js +++ b/Sprint-3/2-practice-tdd/count.test.js @@ -22,3 +22,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 the character does not exist in the string", () => { + const str = "aaaaa"; + const char = "z"; + const count = countChar(str, char); + expect(count).toEqual(0); +}); \ No newline at end of file From 19ad0d2eac8969978360c46feeaa8f95ab142843 Mon Sep 17 00:00:00 2001 From: tasleemadedokun Date: Fri, 6 Mar 2026 14:25:13 +0000 Subject: [PATCH 2/3] Write the function and test to the repeat strings. --- Sprint-3/2-practice-tdd/repeat-str.js | 11 +++++++++-- Sprint-3/2-practice-tdd/repeat-str.test.js | 20 ++++++++++++++++++++ 2 files changed, 29 insertions(+), 2 deletions(-) diff --git a/Sprint-3/2-practice-tdd/repeat-str.js b/Sprint-3/2-practice-tdd/repeat-str.js index 3838c7b003..4ed059cd9a 100644 --- a/Sprint-3/2-practice-tdd/repeat-str.js +++ b/Sprint-3/2-practice-tdd/repeat-str.js @@ -1,5 +1,12 @@ -function repeatStr() { - return "hellohellohello"; +function repeatStr(str, count) { + if (count < 0) { + throw new Error("Count cannot be negative"); + } + + return str.repeat(count); } module.exports = repeatStr; + +console.log(repeatStr("hello", 3)); //hellohellohello +console.log(repeatStr("hello", -3)); //Error: Count cannot be negative \ No newline at end of file diff --git a/Sprint-3/2-practice-tdd/repeat-str.test.js b/Sprint-3/2-practice-tdd/repeat-str.test.js index a3fc1196c4..d005bd7446 100644 --- a/Sprint-3/2-practice-tdd/repeat-str.test.js +++ b/Sprint-3/2-practice-tdd/repeat-str.test.js @@ -21,12 +21,32 @@ 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 when count is 1", () => { + 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 negative", () => { + const str = "hello"; + const count = -1; + expect(() => repeatStr(str, count)).toThrow(); +}); From 4a9765be8c7dbf97f7a251eaeba27181e160a390 Mon Sep 17 00:00:00 2001 From: tasleemadedokun Date: Fri, 6 Mar 2026 15:10:01 +0000 Subject: [PATCH 3/3] Implement the function and test. --- Sprint-3/2-practice-tdd/get-ordinal-number.js | 23 ++++++++++++++- .../2-practice-tdd/get-ordinal-number.test.js | 28 +++++++++++++++++++ 2 files changed, 50 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..8ecdb0da3b 100644 --- a/Sprint-3/2-practice-tdd/get-ordinal-number.js +++ b/Sprint-3/2-practice-tdd/get-ordinal-number.js @@ -1,5 +1,26 @@ + function getOrdinalNumber(num) { - return "1st"; + const absNum = Math.abs(num); // handle negative numbers + const lastTwo = absNum % 100; // get last two digits + const lastDigit = absNum % 10; // get last digit + + if (lastTwo >= 11 && lastTwo <= 13) { + return num + "th"; // exceptions: 11,12,13 + } + + switch (lastDigit) { + case 1: + return num + "st"; + case 2: + return num + "nd"; + case 3: + return num + "rd"; + default: + return num + "th"; + } } +console.log(getOrdinalNumber(12)); //printed 12th. +console.log(getOrdinalNumber(123)); // printed 123rd. +console.log(getOrdinalNumber(52)); // printed 52nd. 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..9010974f6d 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,31 @@ test("should append 'st' for numbers ending with 1, except those ending with 11" expect(getOrdinalNumber(21)).toEqual("21st"); expect(getOrdinalNumber(131)).toEqual("131st"); }); + +// Case 2: Numbers ending with 2 (but not 12) +// When the number ends with 2, except those ending with 12, +// Then the function should return a string by appending "nd" to the number. +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"); +}); + +// Case 3:Numbers ending with 3 (but not 13) +//When the number ends with 3, except those ending with 13, +//Then the function should return a string by appending "rd" to the number. +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"); +}); + +// Case 4: Numbers ending with (0, 4-9,11-13) +// //Then the function should return a string by appending "th" to the number. +test("should append 'th' for numbers ending with 0, 4-9, or 11-13", () => { + expect(getOrdinalNumber(4)).toEqual("4th"); + expect(getOrdinalNumber(10)).toEqual("10th"); + expect(getOrdinalNumber(11)).toEqual("11th"); + expect(getOrdinalNumber(12)).toEqual("12th"); + expect(getOrdinalNumber(13)).toEqual("13th"); +});