From c6aee63b7ef79d93638466e8506439af61d9dc36 Mon Sep 17 00:00:00 2001 From: Darkidd77 Date: Fri, 13 Mar 2026 04:10:32 +0000 Subject: [PATCH 1/3] Implement character counting in countChar function and add test for no occurrences --- Sprint-3/2-practice-tdd/count.js | 9 ++++++++- Sprint-3/2-practice-tdd/count.test.js | 7 +++++++ 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/Sprint-3/2-practice-tdd/count.js b/Sprint-3/2-practice-tdd/count.js index 95b6ebb7d4..fb830054d6 100644 --- a/Sprint-3/2-practice-tdd/count.js +++ b/Sprint-3/2-practice-tdd/count.js @@ -1,5 +1,12 @@ 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; diff --git a/Sprint-3/2-practice-tdd/count.test.js b/Sprint-3/2-practice-tdd/count.test.js index 179ea0ddf7..9ba7069b47 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 show no occurrences of a character", () => { + const str = "aaaaa"; + const char = "c"; + const count = countChar(str, char); + expect(count).toEqual(0); +}); From d961cef23974be91d099027f7f4d300095c71d42 Mon Sep 17 00:00:00 2001 From: Darkidd77 Date: Fri, 13 Mar 2026 04:44:45 +0000 Subject: [PATCH 2/3] Implement getOrdinalNumber function and add comprehensive tests for ordinal suffixes --- Sprint-3/2-practice-tdd/get-ordinal-number.js | 21 ++++++++++- .../2-practice-tdd/get-ordinal-number.test.js | 36 +++++++++++++++++++ Sprint-3/2-practice-tdd/repeat-str.js | 12 +++++-- Sprint-3/2-practice-tdd/repeat-str.test.js | 22 ++++++++++++ 4 files changed, 88 insertions(+), 3 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..ec2a3cdafa 100644 --- a/Sprint-3/2-practice-tdd/get-ordinal-number.js +++ b/Sprint-3/2-practice-tdd/get-ordinal-number.js @@ -1,5 +1,24 @@ function getOrdinalNumber(num) { - return "1st"; + // 1. Handle the 11, 12, 13 exceptions first + if (num === 11 || num === 12 || num === 13) { + return num + "th"; + } + + const lastDigit = num % 10; + + 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 adfa58560f..4a0fb29a53 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,39 @@ 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 11, 12, or 13 +// When the number ends with 11, 12, or 13, +// Then the function should return a string by appending "th" to the number. +test("should append 'th' for numbers ending with 11, 12, or 13", () => { + expect(getOrdinalNumber(11)).toEqual("11th"); + expect(getOrdinalNumber(12)).toEqual("12th"); + expect(getOrdinalNumber(13)).toEqual("13th"); +}); + +// Case 5: Numbers ending with 0, 4, 5, 6, 7, 8, or 9 (but not 10) +// When the number ends with 0, 4, 5, 6, 7, 8, or 9, except those ending with 10, +// Then the function should return a string by appending "th" to the number. +test("should append 'th' for numbers ending with 0, 4-9, except those ending with 10", () => { + expect(getOrdinalNumber(4)).toEqual("4th"); + expect(getOrdinalNumber(10)).toEqual("10th"); + expect(getOrdinalNumber(14)).toEqual("14th"); +}); diff --git a/Sprint-3/2-practice-tdd/repeat-str.js b/Sprint-3/2-practice-tdd/repeat-str.js index 3838c7b003..0227db26cb 100644 --- a/Sprint-3/2-practice-tdd/repeat-str.js +++ b/Sprint-3/2-practice-tdd/repeat-str.js @@ -1,5 +1,13 @@ -function repeatStr() { - return "hellohellohello"; +function repeatStr(str, num) { + if (num < 0) { + throw new Error("Count must be a non-negative integer"); + } + + let result = ""; + for (let i = 0; i < num; 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..f3cad56789 100644 --- a/Sprint-3/2-practice-tdd/repeat-str.test.js +++ b/Sprint-3/2-practice-tdd/repeat-str.test.js @@ -21,12 +21,34 @@ 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( + "Count must be a non-negative integer" + ); +}); From b2a9f100a9286c87aaff17f6c49e9a722ad7240c Mon Sep 17 00:00:00 2001 From: Darkidd77 Date: Sat, 14 Mar 2026 00:41:00 +0000 Subject: [PATCH 3/3] Refactor tests for countChar and getOrdinalNumber functions for clarity and consistency --- Sprint-3/2-practice-tdd/count.test.js | 2 +- Sprint-3/2-practice-tdd/get-ordinal-number.js | 5 +++-- Sprint-3/2-practice-tdd/get-ordinal-number.test.js | 6 ++++-- 3 files changed, 8 insertions(+), 5 deletions(-) diff --git a/Sprint-3/2-practice-tdd/count.test.js b/Sprint-3/2-practice-tdd/count.test.js index 9ba7069b47..603cf0f8e4 100644 --- a/Sprint-3/2-practice-tdd/count.test.js +++ b/Sprint-3/2-practice-tdd/count.test.js @@ -23,7 +23,7 @@ test("should count multiple occurrences of a character", () => { // When the function is called with these inputs, // Then it should return 0, indicating that no occurrences of `char` were found. -test("should show no occurrences of a character", () => { +test("should return 0 when the character is not present in the string", () => { const str = "aaaaa"; const char = "c"; const count = countChar(str, char); diff --git a/Sprint-3/2-practice-tdd/get-ordinal-number.js b/Sprint-3/2-practice-tdd/get-ordinal-number.js index ec2a3cdafa..507bd039f2 100644 --- a/Sprint-3/2-practice-tdd/get-ordinal-number.js +++ b/Sprint-3/2-practice-tdd/get-ordinal-number.js @@ -1,6 +1,7 @@ function getOrdinalNumber(num) { - // 1. Handle the 11, 12, 13 exceptions first - if (num === 11 || num === 12 || num === 13) { + const lastTwoDigits = num % 100; + + if (lastTwoDigits === 11 || lastTwoDigits === 12 || lastTwoDigits === 13) { return num + "th"; } 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 4a0fb29a53..35d8e62b44 100644 --- a/Sprint-3/2-practice-tdd/get-ordinal-number.test.js +++ b/Sprint-3/2-practice-tdd/get-ordinal-number.test.js @@ -44,13 +44,15 @@ test("should append 'th' for numbers ending with 11, 12, or 13", () => { expect(getOrdinalNumber(11)).toEqual("11th"); expect(getOrdinalNumber(12)).toEqual("12th"); expect(getOrdinalNumber(13)).toEqual("13th"); + expect(getOrdinalNumber(1312)).toEqual("1312th"); }); // Case 5: Numbers ending with 0, 4, 5, 6, 7, 8, or 9 (but not 10) // When the number ends with 0, 4, 5, 6, 7, 8, or 9, except those ending with 10, // Then the function should return a string by appending "th" to the number. -test("should append 'th' for numbers ending with 0, 4-9, except those ending with 10", () => { +test("should return 'th' for numbers ending in 0 or 4 through 9", () => { + expect(getOrdinalNumber(0)).toEqual("0th"); expect(getOrdinalNumber(4)).toEqual("4th"); expect(getOrdinalNumber(10)).toEqual("10th"); - expect(getOrdinalNumber(14)).toEqual("14th"); + expect(getOrdinalNumber(110)).toEqual("110th"); });