From 75229ecb0d7cde8b703aae411ec2b9c8eaff559d Mon Sep 17 00:00:00 2001 From: zilinskyte Date: Fri, 7 Nov 2025 22:10:32 +0000 Subject: [PATCH 1/7] count function written and tested --- Sprint-3/2-practice-tdd/count.js | 8 +++++++- Sprint-3/2-practice-tdd/count.test.js | 7 +++++++ 2 files changed, 14 insertions(+), 1 deletion(-) diff --git a/Sprint-3/2-practice-tdd/count.js b/Sprint-3/2-practice-tdd/count.js index 95b6ebb7d..014c6f14c 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 (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 42baf4b4b..f05c1baa8 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 exist in string", () => { + const str = "hello world"; + const char = "a"; + const count = countChar(str, char); + expect(count).toEqual(0); +}); From 555d87e0e915f41970661853fe499584d25e0ac3 Mon Sep 17 00:00:00 2001 From: zilinskyte Date: Fri, 7 Nov 2025 23:20:31 +0000 Subject: [PATCH 2/7] repeat function written and tested --- Sprint-3/2-practice-tdd/repeat.js | 7 +++++-- Sprint-3/2-practice-tdd/repeat.test.js | 20 ++++++++++++++++++++ 2 files changed, 25 insertions(+), 2 deletions(-) diff --git a/Sprint-3/2-practice-tdd/repeat.js b/Sprint-3/2-practice-tdd/repeat.js index 00e60d7f3..8188ac626 100644 --- a/Sprint-3/2-practice-tdd/repeat.js +++ b/Sprint-3/2-practice-tdd/repeat.js @@ -1,5 +1,8 @@ -function repeat() { - return "hellohellohello"; +function repeat(str, count) { + if (!Number.isInteger(count) || count < 0) { + throw Error("Invalid count value"); + } + return str.repeat(count); } 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..37a10d166 100644 --- a/Sprint-3/2-practice-tdd/repeat.test.js +++ b/Sprint-3/2-practice-tdd/repeat.test.js @@ -1,5 +1,6 @@ // Implement a function repeat const repeat = require("./repeat"); + // Given a target string str and a positive integer count, // When the repeat function is called with these inputs, // Then it should: @@ -20,13 +21,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("Invalid count value"); +}); From b57531ac779d831799c95160c17d12c4d335d547 Mon Sep 17 00:00:00 2001 From: zilinskyte Date: Fri, 7 Nov 2025 23:22:43 +0000 Subject: [PATCH 3/7] repeat function written and tested --- Sprint-3/2-practice-tdd/repeat.js | 1 + Sprint-3/2-practice-tdd/repeat.test.js | 1 + 2 files changed, 2 insertions(+) diff --git a/Sprint-3/2-practice-tdd/repeat.js b/Sprint-3/2-practice-tdd/repeat.js index 8188ac626..323313433 100644 --- a/Sprint-3/2-practice-tdd/repeat.js +++ b/Sprint-3/2-practice-tdd/repeat.js @@ -6,3 +6,4 @@ function repeat(str, count) { } module.exports = repeat; + diff --git a/Sprint-3/2-practice-tdd/repeat.test.js b/Sprint-3/2-practice-tdd/repeat.test.js index 37a10d166..dcb47c1cc 100644 --- a/Sprint-3/2-practice-tdd/repeat.test.js +++ b/Sprint-3/2-practice-tdd/repeat.test.js @@ -50,3 +50,4 @@ test("should throw an error for negative count", () => { repeat(str, count); }).toThrow("Invalid count value"); }); + From 7fc5c19c13613cf4f7215d59ec5526104bca295b Mon Sep 17 00:00:00 2001 From: zilinskyte Date: Sat, 8 Nov 2025 00:49:40 +0000 Subject: [PATCH 4/7] ordinal number function written and cases tested --- Sprint-3/2-practice-tdd/get-ordinal-number.js | 9 ++- .../2-practice-tdd/get-ordinal-number.test.js | 56 +++++++++++++++++++ 2 files changed, 64 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..16a005f2b 100644 --- a/Sprint-3/2-practice-tdd/get-ordinal-number.js +++ b/Sprint-3/2-practice-tdd/get-ordinal-number.js @@ -1,5 +1,12 @@ function getOrdinalNumber(num) { - return "1st"; + if (num === 1) return "1st"; + if (num === 2) return "2nd"; + if (num === 3) return "3rd"; + if (num === 11) return "11th"; + if (num === 21) return "21st"; + if (num === 22) return "22nd"; + if (num === 23) return "23rd"; + 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..a4c713b9b 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,59 @@ const getOrdinalNumber = require("./get-ordinal-number"); test("should return '1st' for 1", () => { expect(getOrdinalNumber(1)).toEqual("1st"); }); + +// 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 the ordinal number for 4 +// When the number is 4, +// Then the function should return "4th" + +test("should return '4th' for 4", () => { + expect(getOrdinalNumber(4)).toEqual("4th"); +}); + +// Case 5: Identify the ordinal number for 11 +// When the number is 11, +// Then the function should return "11th" + +test("should return '11th' for 11", () => { + expect(getOrdinalNumber(11)).toEqual("11th"); +}); + +// Case 6: Identify the ordinal number for 21 +// When the number is 21, +// Then the function should return "21st" + +test("should return '21st' for 21", () => { + expect(getOrdinalNumber(21)).toEqual("21st"); +}); + +// Case 7: Identify the ordinal number for 22 +// When the number is 22, +// Then the function should return "22nd" + +test("should return '22nd' for 22", () => { + expect(getOrdinalNumber(22)).toEqual("22nd"); +}); + +// Case 8: Identify the ordinal number for 23 +// When the number is 23, +// Then the function should return "23rd" + +test("should return '23rd' for 23", () => { + expect(getOrdinalNumber(23)).toEqual("23rd"); +}); From 2a5b49dca994139165f4169e882e824bb826666f Mon Sep 17 00:00:00 2001 From: zilinskyte Date: Wed, 12 Nov 2025 18:36:12 +0000 Subject: [PATCH 5/7] prettier formatting --- Sprint-3/2-practice-tdd/count.js | 2 +- Sprint-3/2-practice-tdd/repeat.js | 1 - Sprint-3/2-practice-tdd/repeat.test.js | 1 - 3 files changed, 1 insertion(+), 3 deletions(-) diff --git a/Sprint-3/2-practice-tdd/count.js b/Sprint-3/2-practice-tdd/count.js index 014c6f14c..d59e425d6 100644 --- a/Sprint-3/2-practice-tdd/count.js +++ b/Sprint-3/2-practice-tdd/count.js @@ -5,7 +5,7 @@ function countChar(stringOfCharacters, findCharacter) { count++; } } - return count; + return count; } module.exports = countChar; diff --git a/Sprint-3/2-practice-tdd/repeat.js b/Sprint-3/2-practice-tdd/repeat.js index 323313433..8188ac626 100644 --- a/Sprint-3/2-practice-tdd/repeat.js +++ b/Sprint-3/2-practice-tdd/repeat.js @@ -6,4 +6,3 @@ function repeat(str, count) { } module.exports = repeat; - diff --git a/Sprint-3/2-practice-tdd/repeat.test.js b/Sprint-3/2-practice-tdd/repeat.test.js index dcb47c1cc..37a10d166 100644 --- a/Sprint-3/2-practice-tdd/repeat.test.js +++ b/Sprint-3/2-practice-tdd/repeat.test.js @@ -50,4 +50,3 @@ test("should throw an error for negative count", () => { repeat(str, count); }).toThrow("Invalid count value"); }); - From 1e36baccb8355d6e6d60fc121e217e8aca01982b Mon Sep 17 00:00:00 2001 From: zilinskyte Date: Wed, 12 Nov 2025 22:24:42 +0000 Subject: [PATCH 6/7] simplify getOrdinalNumber function and enhance test cases --- Sprint-3/2-practice-tdd/get-ordinal-number.js | 24 ++++-- .../2-practice-tdd/get-ordinal-number.test.js | 79 ++++++------------- 2 files changed, 40 insertions(+), 63 deletions(-) diff --git a/Sprint-3/2-practice-tdd/get-ordinal-number.js b/Sprint-3/2-practice-tdd/get-ordinal-number.js index 16a005f2b..037c45bd4 100644 --- a/Sprint-3/2-practice-tdd/get-ordinal-number.js +++ b/Sprint-3/2-practice-tdd/get-ordinal-number.js @@ -1,12 +1,20 @@ function getOrdinalNumber(num) { - if (num === 1) return "1st"; - if (num === 2) return "2nd"; - if (num === 3) return "3rd"; - if (num === 11) return "11th"; - if (num === 21) return "21st"; - if (num === 22) return "22nd"; - if (num === 23) return "23rd"; - return num + "th"; + const n = Math.abs(parseInt(num, 10)); + const lastTwoDigits = n % 100; + if (lastTwoDigits >= 11 && lastTwoDigits <= 13) { + return `${n}th`; + } + const lastDigit = n % 10; + switch (lastDigit) { + case 1: + return `${n}st`; + case 2: + return `${n}nd`; + case 3: + return `${n}rd`; + default: + return `${n}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 a4c713b9b..181dfe7a8 100644 --- a/Sprint-3/2-practice-tdd/get-ordinal-number.test.js +++ b/Sprint-3/2-practice-tdd/get-ordinal-number.test.js @@ -4,66 +4,35 @@ const getOrdinalNumber = require("./get-ordinal-number"); // 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", () => { - expect(getOrdinalNumber(1)).toEqual("1st"); -}); - -// 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 1: Numbers ending in 1 except 11 -> "st" +test("should return correct suffix for numbers ending in 1 except 11", () => { + const stNumbers = [1, 21, 101, 121]; + for (const num of stNumbers) { + expect(getOrdinalNumber(num)).toEqual(`${num}st`); + } }); -// 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 2: Numbers ending in 2 except 12 -> "nd" +test("should return correct suffix for numbers ending in 2 except 12", () => { + const ndNumbers = [2, 22, 102, 122]; + for (const num of ndNumbers) { + expect(getOrdinalNumber(num)).toEqual(`${num}nd`); + } }); -// Case 4: Identify the ordinal number for 4 -// When the number is 4, -// Then the function should return "4th" - -test("should return '4th' for 4", () => { - expect(getOrdinalNumber(4)).toEqual("4th"); +// Case 3: Numbers ending in 3 except 13 -> "rd" +test("should return correct suffix for numbers ending in 3 except 13", () => { + const rdNumbers = [3, 23, 103, 123]; + for (const num of rdNumbers) { + expect(getOrdinalNumber(num)).toEqual(`${num}rd`); + } }); -// Case 5: Identify the ordinal number for 11 -// When the number is 11, -// Then the function should return "11th" - -test("should return '11th' for 11", () => { - expect(getOrdinalNumber(11)).toEqual("11th"); +// Case 4: Numbers ending in 4-9, 0, and 11-13 -> "th" +test("should return correct suffix for numbers ending in 4-9, 0, and 11-13", () => { + const thNumbers = [4, 5, 6, 7, 8, 9, 11, 12, 13, 100]; + for (const num of thNumbers) { + expect(getOrdinalNumber(num)).toEqual(`${num}th`); + } }); -// Case 6: Identify the ordinal number for 21 -// When the number is 21, -// Then the function should return "21st" - -test("should return '21st' for 21", () => { - expect(getOrdinalNumber(21)).toEqual("21st"); -}); - -// Case 7: Identify the ordinal number for 22 -// When the number is 22, -// Then the function should return "22nd" - -test("should return '22nd' for 22", () => { - expect(getOrdinalNumber(22)).toEqual("22nd"); -}); - -// Case 8: Identify the ordinal number for 23 -// When the number is 23, -// Then the function should return "23rd" - -test("should return '23rd' for 23", () => { - expect(getOrdinalNumber(23)).toEqual("23rd"); -}); From 0845d0b48fa5cb43c773bcb01018edd58b74ba05 Mon Sep 17 00:00:00 2001 From: zilinskyte Date: Wed, 12 Nov 2025 22:50:40 +0000 Subject: [PATCH 7/7] test cases fixed and passing tests --- Sprint-3/2-practice-tdd/repeat-str.test.js | 32 ++++++++++++---------- 1 file changed, 18 insertions(+), 14 deletions(-) diff --git a/Sprint-3/2-practice-tdd/repeat-str.test.js b/Sprint-3/2-practice-tdd/repeat-str.test.js index 5194e83c9..b88d12d3d 100644 --- a/Sprint-3/2-practice-tdd/repeat-str.test.js +++ b/Sprint-3/2-practice-tdd/repeat-str.test.js @@ -1,11 +1,10 @@ - // 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: +// case 1: 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. @@ -13,40 +12,45 @@ const repeatStr = require("./repeat-str"); test("should repeat the string count times", () => { const str = "hello"; const count = 3; - const repeatedStr = repeatStr(str, count); - expect(repeatedStr).toEqual("hellohellohello"); + expect(repeatStr(str, count)).toEqual("hellohellohello"); }); -// case: handle Count of 1: +// case 2: 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 the original string when count is 1", () => { const str = "hello"; const count = 1; - const repeatedStr = repeat(str, count); - expect(repeatedStr).toEqual("hello"); + expect(repeatStr(str, count)).toEqual("hello"); }); -// case: Handle Count of 0: +// case 3: 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 an empty string when count is 0", () => { const str = "hello"; const count = 0; - const repeatedStr = repeat(str, count); - expect(repeatedStr).toEqual(""); + expect(repeatStr(str, count)).toEqual(""); }); -// case: Negative Count: +// case 4: 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 = -2; - expect(() => { - repeat(str, count); - }).toThrow("Invalid count value"); + expect(() => repeatStr(str, count)).toThrow("Invalid count value"); +}); + +// case 5: Non-integer Count: +// Given a target string str and a non-integer count (e.g., a float or a string), +// When the repeatStr function is called with these inputs, +// Then it should throw an error or return an appropriate error message, as non-integer counts are not valid. +test("should throw an error for non-integer count", () => { + const str = "hello"; + const count = 2.5; + expect(() => repeatStr(str, count)).toThrow("Invalid count value"); });