From 4de3a5efdcd52e542262418c2b90cc4fb460cad2 Mon Sep 17 00:00:00 2001 From: Jak R-S <176810031+jakr-s@users.noreply.github.com> Date: Mon, 1 Dec 2025 14:59:35 +0000 Subject: [PATCH 1/4] implement character counting function and add test for no occurrences --- Sprint-3/2-practice-tdd/count.js | 8 +++++++- Sprint-3/2-practice-tdd/count.test.js | 6 ++++++ 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/Sprint-3/2-practice-tdd/count.js b/Sprint-3/2-practice-tdd/count.js index 95b6ebb7d..22d6c705a 100644 --- a/Sprint-3/2-practice-tdd/count.js +++ b/Sprint-3/2-practice-tdd/count.js @@ -1,5 +1,11 @@ function countChar(stringOfCharacters, findCharacter) { - return 5 + let count = 0; + for (const character of stringOfCharacters) { + if (character === 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 42baf4b4b..a6ce45896 100644 --- a/Sprint-3/2-practice-tdd/count.test.js +++ b/Sprint-3/2-practice-tdd/count.test.js @@ -22,3 +22,9 @@ 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", () => { + const str = "Good morning"; + const char = "z"; + const count = countChar(str, char); + expect(count).toEqual(0); +}); From 728aaee5d2ec6625f3c5380b25cc9d751a6cb19c Mon Sep 17 00:00:00 2001 From: Jak R-S <176810031+jakr-s@users.noreply.github.com> Date: Mon, 1 Dec 2025 15:16:58 +0000 Subject: [PATCH 2/4] implement repeat function and add tests --- Sprint-3/2-practice-tdd/repeat.js | 13 +++++++++++-- Sprint-3/2-practice-tdd/repeat.test.js | 19 +++++++++++++++++++ 2 files changed, 30 insertions(+), 2 deletions(-) diff --git a/Sprint-3/2-practice-tdd/repeat.js b/Sprint-3/2-practice-tdd/repeat.js index 00e60d7f3..109de9eec 100644 --- a/Sprint-3/2-practice-tdd/repeat.js +++ b/Sprint-3/2-practice-tdd/repeat.js @@ -1,5 +1,14 @@ -function repeat() { - return "hellohellohello"; +function repeat(str, count) { + let result = ""; + + if (count < 0) { + throw new Error("Count must be a non-negative integer"); + } + + for (let i = 0; i < count; i++) { + result += str; + } + return result; } module.exports = repeat; diff --git a/Sprint-3/2-practice-tdd/repeat.test.js b/Sprint-3/2-practice-tdd/repeat.test.js index 34097b09c..b591f8a71 100644 --- a/Sprint-3/2-practice-tdd/repeat.test.js +++ b/Sprint-3/2-practice-tdd/repeat.test.js @@ -20,13 +20,32 @@ test("should repeat the string count times", () => { // Given a target string str and a count equal to 1, // When the repeat 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 the original string when count is 1", () => { + const str = "hello"; + const count = 1; + const repeatedStr = repeat(str, count); + expect(repeatedStr).toEqual("hello"); +}); // case: Handle Count of 0: // Given a target string str and a count equal to 0, // When the repeat 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 an empty string when count is 0", () => { + const str = "hello"; + const count = 0; + const repeatedStr = repeat(str, count); + expect(repeatedStr).toEqual(""); +}); // case: Negative Count: // Given a target string str and a negative integer count, // When the repeat 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 = -2; + expect(() => { + repeat(str, count); + }).toThrow("Count must be a non-negative integer"); +}); From cb9b03ece58d9d4de7e209fa42b44db3553396b7 Mon Sep 17 00:00:00 2001 From: Jak R-S <176810031+jakr-s@users.noreply.github.com> Date: Mon, 1 Dec 2025 16:28:44 +0000 Subject: [PATCH 3/4] implement getOrdinalNumber function and add tests --- Sprint-3/2-practice-tdd/get-ordinal-number.js | 18 +++++++++++- .../2-practice-tdd/get-ordinal-number.test.js | 28 +++++++++++++++++++ 2 files changed, 45 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 f95d71db1..acffdf7bc 100644 --- a/Sprint-3/2-practice-tdd/get-ordinal-number.js +++ b/Sprint-3/2-practice-tdd/get-ordinal-number.js @@ -1,5 +1,21 @@ function getOrdinalNumber(num) { - return "1st"; + const remainderTen = num % 10; + const remainderHundred = num % 100; + + if (remainderHundred >= 11 && remainderHundred <= 13) { + return `${num}th`; + } + + switch (remainderTen) { + 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 dfe4b6091..2da9854e2 100644 --- a/Sprint-3/2-practice-tdd/get-ordinal-number.test.js +++ b/Sprint-3/2-practice-tdd/get-ordinal-number.test.js @@ -11,3 +11,31 @@ const getOrdinalNumber = require("./get-ordinal-number"); test("should return '1st' for 1", () => { expect(getOrdinalNumber(1)).toEqual("1st"); }); + +test("should return '2nd' for 2", () => { + expect(getOrdinalNumber(2)).toEqual("2nd"); +}); + +test("should return '3rd' for 3", () => { + expect(getOrdinalNumber(3)).toEqual("3rd"); +}); + +test("should return '4th' for 4", () => { + expect(getOrdinalNumber(4)).toEqual("4th"); +}); + +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 '111th' for 111", () => { + expect(getOrdinalNumber(111)).toEqual("111th"); +}); From 8bcfee08b72e401ea576a301767b7da22048a17c Mon Sep 17 00:00:00 2001 From: Jak R-S <176810031+jakr-s@users.noreply.github.com> Date: Wed, 3 Dec 2025 20:27:54 +0000 Subject: [PATCH 4/4] refactor(tests): improve structure of ordinal number tests --- .../2-practice-tdd/get-ordinal-number.test.js | 41 ++++++++----------- 1 file changed, 17 insertions(+), 24 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 2da9854e2..0dc56cd71 100644 --- a/Sprint-3/2-practice-tdd/get-ordinal-number.test.js +++ b/Sprint-3/2-practice-tdd/get-ordinal-number.test.js @@ -1,41 +1,34 @@ 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 - -// Case 1: Identify the ordinal number for 1 -// When the number is 1, -// Then the function should return "1st" - -test("should return '1st' for 1", () => { +// Test for numbers ending in 1, except those ending in 11 +test("append 'st' to numbers ending in 1, except those ending in 11", () => { expect(getOrdinalNumber(1)).toEqual("1st"); + expect(getOrdinalNumber(21)).toEqual("21st"); + expect(getOrdinalNumber(31)).toEqual("31st"); }); -test("should return '2nd' for 2", () => { +// Test for numbers ending in 2, except those ending in 12 +test("append 'nd' to numbers ending in 2, except those ending in 12", () => { expect(getOrdinalNumber(2)).toEqual("2nd"); + expect(getOrdinalNumber(22)).toEqual("22nd"); + expect(getOrdinalNumber(132)).toEqual("132nd"); }); -test("should return '3rd' for 3", () => { +// Test for numbers ending in 3, except those ending in 13 +test("append 'rd' to numbers ending in 3, except those ending in 13", () => { expect(getOrdinalNumber(3)).toEqual("3rd"); + expect(getOrdinalNumber(23)).toEqual("23rd"); + expect(getOrdinalNumber(133)).toEqual("133rd"); }); -test("should return '4th' for 4", () => { +// Test for numbers ending in 0, 4, 5, 6, 7, 8, 9, and those ending in 11, 12, 13 +test("append 'th' to numbers ending in 0, 4, 5, 6, 7, 8, 9, and those ending in 11, 12, 13", () => { expect(getOrdinalNumber(4)).toEqual("4th"); -}); - -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 '111th' for 111", () => { + expect(getOrdinalNumber(14)).toEqual("14th"); + expect(getOrdinalNumber(20)).toEqual("20th"); + expect(getOrdinalNumber(100)).toEqual("100th"); expect(getOrdinalNumber(111)).toEqual("111th"); });