From 951157ca911103ff7cfce39eac3de96f153dc64d Mon Sep 17 00:00:00 2001 From: Ali Date: Fri, 14 Nov 2025 21:32:57 +0000 Subject: [PATCH 1/6] Add test case for character not found in string count.test.js --- Sprint-3/2-practice-tdd/count.test.js | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/Sprint-3/2-practice-tdd/count.test.js b/Sprint-3/2-practice-tdd/count.test.js index 42baf4b4b..2de7c7b10 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 the case-sensitive str, // When the function is called with these inputs, // Then it should return 0, indicating that no occurrences of the char were found in the case-sensitive str. + +test("should return 0 when character does not occur in the string", () => { + const str = "hello"; + const char = "z"; + const count = countChar(str, char); + expect(count).toEqual(0); +}); From f774e8db1c86c4c20486bc0b501dfabef273e49a Mon Sep 17 00:00:00 2001 From: Ali Date: Fri, 14 Nov 2025 21:43:26 +0000 Subject: [PATCH 2/6] Implement character counting logic in countChar function --- Sprint-3/2-practice-tdd/count.js | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/Sprint-3/2-practice-tdd/count.js b/Sprint-3/2-practice-tdd/count.js index 95b6ebb7d..f973f02be 100644 --- a/Sprint-3/2-practice-tdd/count.js +++ b/Sprint-3/2-practice-tdd/count.js @@ -1,5 +1,14 @@ function countChar(stringOfCharacters, findCharacter) { - return 5 + let count = 0; + + for (let char of stringOfCharacters) { + if (char === findCharacter) { + count++; + } + } + + return count; } module.exports = countChar; + From 150a497a71a3cd5d3023123e918e8f17d3195fd2 Mon Sep 17 00:00:00 2001 From: Ali Date: Fri, 14 Nov 2025 21:47:39 +0000 Subject: [PATCH 3/6] Implement getOrdinalNumber function to return correct ordinal suffixes --- Sprint-3/2-practice-tdd/get-ordinal-number.js | 14 ++++- .../2-practice-tdd/get-ordinal-number.test.js | 62 ++++++++++++++++--- 2 files changed, 66 insertions(+), 10 deletions(-) diff --git a/Sprint-3/2-practice-tdd/get-ordinal-number.js b/Sprint-3/2-practice-tdd/get-ordinal-number.js index f95d71db1..46528f0b6 100644 --- a/Sprint-3/2-practice-tdd/get-ordinal-number.js +++ b/Sprint-3/2-practice-tdd/get-ordinal-number.js @@ -1,5 +1,17 @@ function getOrdinalNumber(num) { - return "1st"; + const lastDigit = num % 10; + const lastTwoDigits = num % 100; + + if (lastTwoDigits >= 11 && lastTwoDigits <= 13) { + return `${num}th`; + } + + + 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 dfe4b6091..6f97cee81 100644 --- a/Sprint-3/2-practice-tdd/get-ordinal-number.test.js +++ b/Sprint-3/2-practice-tdd/get-ordinal-number.test.js @@ -1,13 +1,57 @@ -const getOrdinalNumber = require("./get-ordinal-number"); -// In this week's prep, we started implementing getOrdinalNumber +// Case 2: Identify the ordinal number for 2 +// When the number is 2, +// Then the function should return "2nd" +test("should return '2nd' for 2", () => { + expect(getOrdinalNumber(2)).toEqual("2nd"); +}); + +// Case 3: Identify the ordinal number for 3 +// When the number is 3, +// Then the function should return "3rd" +test("should return '3rd' for 3", () => { + expect(getOrdinalNumber(3)).toEqual("3rd"); +}); + +// Case 4: Identify ordinal numbers ending in 1 but not 11 +// When the number ends with 1 but is not 11, +// Then the function should return the correct 'st' suffix +test("should return '21st' for 21", () => { + expect(getOrdinalNumber(21)).toEqual("21st"); +}); + +// Case 5: Identify ordinal numbers ending in 2 but not 12 +// When the number ends with 2 but is not 12, +// Then the function should return the correct 'nd' suffix +test("should return '42nd' for 42", () => { + expect(getOrdinalNumber(42)).toEqual("42nd"); +}); -// continue testing and implementing getOrdinalNumber for additional cases -// Write your tests using Jest - remember to run your tests often for continual feedback +// Case 6: Identify ordinal numbers ending in 3 but not 13 +// When the number ends with 3 but is not 13, +// Then the function should return the correct 'rd' suffix +test("should return '53rd' for 53", () => { + expect(getOrdinalNumber(53)).toEqual("53rd"); +}); -// Case 1: Identify the ordinal number for 1 -// When the number is 1, -// Then the function should return "1st" +// Case 7: Identify teen exceptions 11, 12, 13 +// When the number is 11, 12, or 13, +// Then the function should return the correct 'th' suffix +test("should return '11th' for 11", () => { + expect(getOrdinalNumber(11)).toEqual("11th"); +}); +test("should return '12th' for 12", () => { + expect(getOrdinalNumber(12)).toEqual("12th"); +}); +test("should return '13th' for 13", () => { + expect(getOrdinalNumber(13)).toEqual("13th"); +}); -test("should return '1st' for 1", () => { - expect(getOrdinalNumber(1)).toEqual("1st"); +// Case 8: Identify general 'th' endings +// When the number does not end in 1, 2, or 3 (except teens), +// Then the function should return the 'th' suffix +test("should return '20th' for 20", () => { + expect(getOrdinalNumber(20)).toEqual("20th"); +}); +test("should return '100th' for 100", () => { + expect(getOrdinalNumber(100)).toEqual("100th"); }); From 9722cdea84bc1c0c7f957ef593cb8168f6a14fa9 Mon Sep 17 00:00:00 2001 From: Ali Date: Fri, 14 Nov 2025 21:49:55 +0000 Subject: [PATCH 4/6] Refactor repeatStr function to handle negative counts and return repeated string --- Sprint-3/2-practice-tdd/repeat-str.js | 7 +++-- Sprint-3/2-practice-tdd/repeat-str.test.js | 35 +++++++++++----------- 2 files changed, 22 insertions(+), 20 deletions(-) diff --git a/Sprint-3/2-practice-tdd/repeat-str.js b/Sprint-3/2-practice-tdd/repeat-str.js index 3838c7b00..9dee7447e 100644 --- a/Sprint-3/2-practice-tdd/repeat-str.js +++ b/Sprint-3/2-practice-tdd/repeat-str.js @@ -1,5 +1,8 @@ -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; diff --git a/Sprint-3/2-practice-tdd/repeat-str.test.js b/Sprint-3/2-practice-tdd/repeat-str.test.js index fc59d019e..e53525dba 100644 --- a/Sprint-3/2-practice-tdd/repeat-str.test.js +++ b/Sprint-3/2-practice-tdd/repeat-str.test.js @@ -1,32 +1,31 @@ -// Implement a function repeatStr -const repeatStr = require("./repeat-str"); -// Given a target string str and a positive integer count, -// When the repeatStr function is called with these inputs, -// Then it should: - -// case: repeat String: -// Given a target string str and a positive integer count, -// When the repeatStr function is called with these inputs, -// Then it should repeat the str count times and return a new string containing the repeated str values. - -test("should repeat the string count times", () => { - const str = "hello"; - const count = 3; - const repeatedStr = repeatStr(str, count); - expect(repeatedStr).toEqual("hellohellohello"); -}); - // case: handle Count of 1: // 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, ensuring that a count of 1 results in no repetition. +test("should return 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, ensuring that a count of 0 results in an empty output. +test("should return empty string when count is 0", () => { + const str = "hello"; + const count = 0; + const repeatedStr = repeatStr(str, count); + expect(repeatedStr).toEqual(""); +}); // case: 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 or return an appropriate error message, as negative counts are not valid. +test("should throw an error for negative count", () => { + const str = "hello"; + const count = -3; + expect(() => repeatStr(str, count)).toThrow(); +}); From 0e92f6220def06db212f882f6836da1d115fd10b Mon Sep 17 00:00:00 2001 From: Ali Date: Sat, 15 Nov 2025 16:14:12 +0000 Subject: [PATCH 5/6] Refactor tests in getOrdinalNumber to streamline cases and improve clarity --- .../2-practice-tdd/get-ordinal-number.test.js | 62 +++---------------- 1 file changed, 9 insertions(+), 53 deletions(-) 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 6f97cee81..dfe4b6091 100644 --- a/Sprint-3/2-practice-tdd/get-ordinal-number.test.js +++ b/Sprint-3/2-practice-tdd/get-ordinal-number.test.js @@ -1,57 +1,13 @@ -// Case 2: Identify the ordinal number for 2 -// When the number is 2, -// Then the function should return "2nd" -test("should return '2nd' for 2", () => { - expect(getOrdinalNumber(2)).toEqual("2nd"); -}); - -// Case 3: Identify the ordinal number for 3 -// When the number is 3, -// Then the function should return "3rd" -test("should return '3rd' for 3", () => { - expect(getOrdinalNumber(3)).toEqual("3rd"); -}); - -// Case 4: Identify ordinal numbers ending in 1 but not 11 -// When the number ends with 1 but is not 11, -// Then the function should return the correct 'st' suffix -test("should return '21st' for 21", () => { - expect(getOrdinalNumber(21)).toEqual("21st"); -}); - -// Case 5: Identify ordinal numbers ending in 2 but not 12 -// When the number ends with 2 but is not 12, -// Then the function should return the correct 'nd' suffix -test("should return '42nd' for 42", () => { - expect(getOrdinalNumber(42)).toEqual("42nd"); -}); +const getOrdinalNumber = require("./get-ordinal-number"); +// In this week's prep, we started implementing getOrdinalNumber -// Case 6: Identify ordinal numbers ending in 3 but not 13 -// When the number ends with 3 but is not 13, -// Then the function should return the correct 'rd' suffix -test("should return '53rd' for 53", () => { - expect(getOrdinalNumber(53)).toEqual("53rd"); -}); +// continue testing and implementing getOrdinalNumber for additional cases +// Write your tests using Jest - remember to run your tests often for continual feedback -// Case 7: Identify teen exceptions 11, 12, 13 -// When the number is 11, 12, or 13, -// Then the function should return the correct 'th' suffix -test("should return '11th' for 11", () => { - expect(getOrdinalNumber(11)).toEqual("11th"); -}); -test("should return '12th' for 12", () => { - expect(getOrdinalNumber(12)).toEqual("12th"); -}); -test("should return '13th' for 13", () => { - expect(getOrdinalNumber(13)).toEqual("13th"); -}); +// Case 1: Identify the ordinal number for 1 +// When the number is 1, +// Then the function should return "1st" -// Case 8: Identify general 'th' endings -// When the number does not end in 1, 2, or 3 (except teens), -// Then the function should return the 'th' suffix -test("should return '20th' for 20", () => { - expect(getOrdinalNumber(20)).toEqual("20th"); -}); -test("should return '100th' for 100", () => { - expect(getOrdinalNumber(100)).toEqual("100th"); +test("should return '1st' for 1", () => { + expect(getOrdinalNumber(1)).toEqual("1st"); }); From 60864d031f91b323727ef21d94b34957d81f04fd Mon Sep 17 00:00:00 2001 From: Ali Date: Sat, 15 Nov 2025 16:31:18 +0000 Subject: [PATCH 6/6] Refactor tests in getOrdinalNumber to cover additional cases and improve error handling --- .../2-practice-tdd/get-ordinal-number.test.js | 58 ++++++++++++++++--- 1 file changed, 50 insertions(+), 8 deletions(-) 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 dfe4b6091..ffc75860d 100644 --- a/Sprint-3/2-practice-tdd/get-ordinal-number.test.js +++ b/Sprint-3/2-practice-tdd/get-ordinal-number.test.js @@ -1,13 +1,55 @@ const getOrdinalNumber = require("./get-ordinal-number"); -// In this week's prep, we started implementing getOrdinalNumber -// continue testing and implementing getOrdinalNumber for additional cases -// Write your tests using Jest - remember to run your tests often for continual feedback +// Category 1: Numbers ending in 11, 12, 13 → always "th" +test("appends 'th' to numbers ending in 11, 12, or 13", () => { + const numbers = [11, 12, 13, 111, 1012, 1313]; + numbers.forEach(num => { + expect(getOrdinalNumber(num)).toEqual(`${num}th`); + }); +}); + +// Category 2: Numbers ending in 1 → "st" (except those ending in 11) +test("appends 'st' to numbers ending in 1 except those ending in 11", () => { + const numbers = [1, 21, 101, 1001]; + numbers.forEach(num => { + expect(getOrdinalNumber(num)).toEqual(`${num}st`); + }); +}); -// Case 1: Identify the ordinal number for 1 -// When the number is 1, -// Then the function should return "1st" +// Category 3: Numbers ending in 2 → "nd" (except those ending in 12) +test("appends 'nd' to numbers ending in 2 except those ending in 12", () => { + const numbers = [2, 22, 102, 202]; + numbers.forEach(num => { + expect(getOrdinalNumber(num)).toEqual(`${num}nd`); + }); +}); + +// Category 4: Numbers ending in 3 → "rd" (except those ending in 13) +test("appends 'rd' to numbers ending in 3 except those ending in 13", () => { + const numbers = [3, 23, 103, 1003]; + numbers.forEach(num => { + expect(getOrdinalNumber(num)).toEqual(`${num}rd`); + }); +}); + +// Category 5: All other numbers → "th" +test("appends 'th' to numbers that don't meet special suffix rules", () => { + const numbers = [4, 5, 6, 9, 20, 100, 1004, 1009]; + numbers.forEach(num => { + expect(getOrdinalNumber(num)).toEqual(`${num}th`); + }); +}); + +// Error handling - Non-integers +test("throws an error when number is not an integer", () => { + const invalidInputs = [1.5, "3", null, undefined, {}, [], NaN]; + + invalidInputs.forEach(input => { + expect(() => getOrdinalNumber(input)).toThrow("Input must be an integer"); + }); +}); -test("should return '1st' for 1", () => { - expect(getOrdinalNumber(1)).toEqual("1st"); +// Error handling - Negative values +test("throws an error when number is negative", () => { + expect(() => getOrdinalNumber(-1)).toThrow("Input must be a non-negative integer"); });