From f16867510d6e3d61a4929b1be6ea42a8293954e0 Mon Sep 17 00:00:00 2001 From: FarancisGH Date: Sat, 7 Mar 2026 20:51:36 +0000 Subject: [PATCH 1/2] Adjustments made on the 2-practice-tdd files to implement the repeatStr function and add test cases for it. The repeatStr function now uses the built-in `repeat` method to return the string repeated a specified number of times. Additionally, test cases have been added to verify the behavior of the function when the count is 1, 0, and negative. --- Sprint-3/2-practice-tdd/count.js | 17 +++++++++++ Sprint-3/2-practice-tdd/count.test.js | 7 +++++ Sprint-3/2-practice-tdd/get-ordinal-number.js | 25 ++++++++++++++++ .../2-practice-tdd/get-ordinal-number.test.js | 29 +++++++++++++++++++ Sprint-3/2-practice-tdd/repeat-str.js | 8 +++++ Sprint-3/2-practice-tdd/repeat-str.test.js | 17 +++++++++++ 6 files changed, 103 insertions(+) diff --git a/Sprint-3/2-practice-tdd/count.js b/Sprint-3/2-practice-tdd/count.js index 95b6ebb7d4..a85964aac4 100644 --- a/Sprint-3/2-practice-tdd/count.js +++ b/Sprint-3/2-practice-tdd/count.js @@ -3,3 +3,20 @@ function countChar(stringOfCharacters, findCharacter) { } module.exports = countChar; +// + +//Below is the implementation of the countChar function that counts the number of times a character occurs in a string. +// The function takes two parameters: `stringOfCharacters`, which is the string to search through, and `findCharacter`, which is the character to count. +// The function initializes a counter to 0 and iterates through each character in the string. If the current character matches `findCharacter`, the counter is incremented. Finally, the function returns the total count. + +function countChar(stringOfCharacters, findCharacter) { + let count = 0; + for (let i = 0; i < stringOfCharacters.length; i++) { + if (stringOfCharacters[i] === findCharacter) { + count++; + } + } + return count; +} + +module.exports = countChar; \ 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..2bff3092de 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 return 0 when the character does not occur in the string", () => { + const str = "hello world"; + const char = "x"; + 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..736aa0ce97 100644 --- a/Sprint-3/2-practice-tdd/get-ordinal-number.js +++ b/Sprint-3/2-practice-tdd/get-ordinal-number.js @@ -3,3 +3,28 @@ function getOrdinalNumber(num) { } module.exports = getOrdinalNumber; +// Below is the implementation of the getOrdinalNumber function that converts a number to its ordinal form (e.g., 1 to "1st", 2 to "2nd", etc.). +// The function takes a single parameter `num`, which is the number to convert. +// It checks the last digit of the number to determine the appropriate suffix ("st", "nd", "rd", or "th") and returns the number concatenated with its ordinal suffix as a string. + +function getOrdinalNumber(num) { + const lastDigit = num % 10; + const lastTwoDigits = num % 100; + + if (lastTwoDigits >= 11 && lastTwoDigits <= 13) { + return num + "th"; + } + + switch (lastDigit) { + case 1: + return num + "st"; + case 2: + return num + "nd"; + case 3: + return num + "rd"; + default: + 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..731164a7c2 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,32 @@ 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, or 10-20 (including 11-19) +// When the number ends with 0, 4-9, or 10-20 (including 11-19), +// 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 10-20 (including 11-19)", () => { + expect(getOrdinalNumber(0)).toEqual("0th"); + expect(getOrdinalNumber(4)).toEqual("4th"); + expect(getOrdinalNumber(5)).toEqual("5th"); + expect(getOrdinalNumber(10)).toEqual("10th"); + expect(getOrdinalNumber(11)).toEqual("11th"); + expect(getOrdinalNumber(12)).toEqual("12th"); + expect(getOrdinalNumber(13)).toEqual("13th"); + expect(getOrdinalNumber(20)).toEqual("20th"); +}); diff --git a/Sprint-3/2-practice-tdd/repeat-str.js b/Sprint-3/2-practice-tdd/repeat-str.js index 3838c7b003..c6fe9a1892 100644 --- a/Sprint-3/2-practice-tdd/repeat-str.js +++ b/Sprint-3/2-practice-tdd/repeat-str.js @@ -3,3 +3,11 @@ function repeatStr() { } module.exports = repeatStr; +// Below is the implementation of the repeatStr function that takes a string and a number as parameters and returns the string repeated the specified number of times. +// The function uses the built-in `repeat` method of strings to achieve this. It takes the input string and repeats it `n` times, where `n` is the number provided as the second parameter. + +function repeatStr(str, n) { + return str.repeat(n); +} + +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..771a1e2a6d 100644 --- a/Sprint-3/2-practice-tdd/repeat-str.test.js +++ b/Sprint-3/2-practice-tdd/repeat-str.test.js @@ -20,13 +20,30 @@ test("should repeat the string count times", () => { // 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. +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("Count must be a non-negative integer"); +}); From 7c05e43c934fa07fddc4184700d81d849f5e1c2d Mon Sep 17 00:00:00 2001 From: FarancisGH Date: Sat, 7 Mar 2026 21:15:50 +0000 Subject: [PATCH 2/2] further changes to be committed: --- Sprint-3/2-practice-tdd/repeat-str.js | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/Sprint-3/2-practice-tdd/repeat-str.js b/Sprint-3/2-practice-tdd/repeat-str.js index c6fe9a1892..b4bd99da2b 100644 --- a/Sprint-3/2-practice-tdd/repeat-str.js +++ b/Sprint-3/2-practice-tdd/repeat-str.js @@ -6,8 +6,9 @@ module.exports = repeatStr; // Below is the implementation of the repeatStr function that takes a string and a number as parameters and returns the string repeated the specified number of times. // The function uses the built-in `repeat` method of strings to achieve this. It takes the input string and repeats it `n` times, where `n` is the number provided as the second parameter. -function repeatStr(str, n) { + +function repeatStr(n, str) { return str.repeat(n); } -module.exports = repeatStr; +module.exports = repeatStr; \ No newline at end of file