From 92afa66deab0bcdeb8009914ee30833de7010d60 Mon Sep 17 00:00:00 2001 From: Payman IB Date: Sat, 15 Nov 2025 14:28:30 +0000 Subject: [PATCH 01/11] Implement countChar function and add test for non-existent character --- Project-CLI-Treasure-Hunt | 1 + Sprint-3/2-practice-tdd/count.js | 11 +++++++++-- Sprint-3/2-practice-tdd/count.test.js | 7 +++++++ 3 files changed, 17 insertions(+), 2 deletions(-) create mode 160000 Project-CLI-Treasure-Hunt diff --git a/Project-CLI-Treasure-Hunt b/Project-CLI-Treasure-Hunt new file mode 160000 index 000000000..6668c42dc --- /dev/null +++ b/Project-CLI-Treasure-Hunt @@ -0,0 +1 @@ +Subproject commit 6668c42dc5412e54026ca0fa8cb5691017ef6350 diff --git a/Sprint-3/2-practice-tdd/count.js b/Sprint-3/2-practice-tdd/count.js index 95b6ebb7d..aff5f239a 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; +} +console.log(countChar("hello world!", "l")); 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..1b29c1edf 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 the string", () => { + const str = "hello world"; + const char = "z"; + const count = countChar(str, char); + expect(count).toEqual(0); +}); \ No newline at end of file From 9e894a7150330d9f471410f379479a58470a4757 Mon Sep 17 00:00:00 2001 From: Payman IB Date: Sat, 15 Nov 2025 14:53:16 +0000 Subject: [PATCH 02/11] Refactor getOrdinalNumber function and expand test cases for ordinal numbers --- Sprint-3/2-practice-tdd/get-ordinal-number.js | 10 +++++++++- Sprint-3/2-practice-tdd/get-ordinal-number.test.js | 14 ++++++++++++++ 2 files changed, 23 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..97debb0df 100644 --- a/Sprint-3/2-practice-tdd/get-ordinal-number.js +++ b/Sprint-3/2-practice-tdd/get-ordinal-number.js @@ -1,5 +1,13 @@ function getOrdinalNumber(num) { - return "1st"; + const suffixes = ["th", "st", "nd", "rd"]; + const v = num % 100; + console.log(v); + if (v >= 11 && v <= 13) { + return num + "th"; + } + const suffix = suffixes[(v % 10)] || "th"; + return num + suffix; } +console.log(getOrdinalNumber(52)); 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..ecd327623 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,17 @@ 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 '24th' for 24", () => { + expect(getOrdinalNumber(24)).toEqual("24th"); +}); +test("should return '11th' for 11", () => { + expect(getOrdinalNumber(11)).toEqual("11th"); +}); \ No newline at end of file From ded405ec9b5625276faed0eb38c944ff265e7b15 Mon Sep 17 00:00:00 2001 From: Payman IB Date: Sat, 15 Nov 2025 15:29:41 +0000 Subject: [PATCH 03/11] Implement repeat function with error handling for negative counts and add corresponding tests --- Sprint-3/2-practice-tdd/repeat.js | 15 +++++++++++++-- Sprint-3/2-practice-tdd/repeat.test.js | 24 ++++++++++++++++++------ 2 files changed, 31 insertions(+), 8 deletions(-) diff --git a/Sprint-3/2-practice-tdd/repeat.js b/Sprint-3/2-practice-tdd/repeat.js index 00e60d7f3..685e3f3cd 100644 --- a/Sprint-3/2-practice-tdd/repeat.js +++ b/Sprint-3/2-practice-tdd/repeat.js @@ -1,5 +1,16 @@ -function repeat() { - return "hellohellohello"; +function repeat(str , count) { + if (count < 0) { + return "Count must be a non-negative integer"; + } + if (count === 0) { + return ""; + } + for (let i = 0; i < count; i++) { + return str.repeat(count); + } } + console.log(repeat("hello", -2)); + + 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..8d9611f6a 100644 --- a/Sprint-3/2-practice-tdd/repeat.test.js +++ b/Sprint-3/2-practice-tdd/repeat.test.js @@ -1,9 +1,5 @@ // 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: - // case: repeat String: // Given a target string str and a positive integer count, // When the repeat function is called with these inputs, @@ -20,13 +16,29 @@ 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 = "world"; + const count = 1; + const repeatedStr = repeat(str, count); + expect(repeatedStr).toEqual("world"); +}); // 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 = "test"; + 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 return a message for negative count", () => { + const str = "error"; + const count = -2; + const repeatedStr = repeat(str, count); + expect(repeatedStr).toEqual("Count must be a non-negative integer"); +}); \ No newline at end of file From a124398d6b9366ba0c2c9086f2ce09b400000d2e Mon Sep 17 00:00:00 2001 From: Payman IB Date: Sat, 22 Nov 2025 12:27:59 +0000 Subject: [PATCH 04/11] Refactor getOrdinalNumber function to improve variable naming and remove unnecessary console logs --- Sprint-3/2-practice-tdd/get-ordinal-number.js | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/Sprint-3/2-practice-tdd/get-ordinal-number.js b/Sprint-3/2-practice-tdd/get-ordinal-number.js index 97debb0df..e6b2c2098 100644 --- a/Sprint-3/2-practice-tdd/get-ordinal-number.js +++ b/Sprint-3/2-practice-tdd/get-ordinal-number.js @@ -1,13 +1,12 @@ function getOrdinalNumber(num) { const suffixes = ["th", "st", "nd", "rd"]; - const v = num % 100; - console.log(v); - if (v >= 11 && v <= 13) { + const date = num % 100; + + if (date >= 11 && date <= 13) { return num + "th"; } - const suffix = suffixes[(v % 10)] || "th"; + const suffix = suffixes[(date % 10)] || "th"; return num + suffix; } -console.log(getOrdinalNumber(52)); module.exports = getOrdinalNumber; From e7d7fffda0a266fdf4425ec95edad3cee6540bfc Mon Sep 17 00:00:00 2001 From: Payman IB Date: Sat, 22 Nov 2025 13:02:33 +0000 Subject: [PATCH 05/11] Update getCardValue function to check for specific card representation and enhance ordinal number tests with clearer descriptions and additional cases --- .../implement/3-get-card-value.js | 2 +- .../2-practice-tdd/get-ordinal-number.test.js | 21 ++++++++++++------- 2 files changed, 15 insertions(+), 8 deletions(-) diff --git a/Sprint-3/1-implement-and-rewrite-tests/implement/3-get-card-value.js b/Sprint-3/1-implement-and-rewrite-tests/implement/3-get-card-value.js index 266525d1b..7268ef0fb 100644 --- a/Sprint-3/1-implement-and-rewrite-tests/implement/3-get-card-value.js +++ b/Sprint-3/1-implement-and-rewrite-tests/implement/3-get-card-value.js @@ -8,7 +8,7 @@ // write one test at a time, and make it pass, build your solution up methodically // just make one change at a time -- don't rush -- programmers are deep and careful thinkers function getCardValue(card) { - if (rank === "A") { + if (card === "A♠") { return 11; } } 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 ecd327623..db284920d 100644 --- a/Sprint-3/2-practice-tdd/get-ordinal-number.test.js +++ b/Sprint-3/2-practice-tdd/get-ordinal-number.test.js @@ -8,19 +8,26 @@ const getOrdinalNumber = require("./get-ordinal-number"); // When the number is 1, // Then the function should return "1st" -test("should return '1st' for 1", () => { +test("append 'nd' to numbers ending in 1, except those ending in 11", () => { expect(getOrdinalNumber(1)).toEqual("1st"); + expect( getOrdinalNumber(21) ).toEqual("21st"); + expect( getOrdinalNumber(131) ).toEqual("131st"); }); -test("should return '2nd' for 2", () => { - expect(getOrdinalNumber(2)).toEqual("2nd"); +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("append 'rd' to numbers ending in 3, except those ending in 13", () => { expect(getOrdinalNumber(3)).toEqual("3rd"); + expect( getOrdinalNumber(33) ).toEqual("33rd"); + expect( getOrdinalNumber(133) ).toEqual("133rd"); }); -test("should return '24th' for 24", () => { - expect(getOrdinalNumber(24)).toEqual("24th"); +test("append 'th' to numbers ending in 4, except those ending in 14", () => { + expect(getOrdinalNumber(4)).toEqual("4th"); + expect( getOrdinalNumber(24) ).toEqual("24th"); + expect( getOrdinalNumber(134) ).toEqual("134th"); }); test("should return '11th' for 11", () => { expect(getOrdinalNumber(11)).toEqual("11th"); From fbacf4a5ed60b63a177aecd70ede266ec4c17517 Mon Sep 17 00:00:00 2001 From: Payman IB Date: Sat, 22 Nov 2025 13:10:04 +0000 Subject: [PATCH 06/11] reverted to original --- Sprint-2/1-key-errors/0.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Sprint-2/1-key-errors/0.js b/Sprint-2/1-key-errors/0.js index 653d6f5a0..2445d911f 100644 --- a/Sprint-2/1-key-errors/0.js +++ b/Sprint-2/1-key-errors/0.js @@ -10,4 +10,4 @@ function capitalise(str) { } // =============> write your explanation here -// =============> write your new code here +// =============> write your new code here \ No newline at end of file From 3585be72738794e21e09a204db52e099087cb26f Mon Sep 17 00:00:00 2001 From: Payman IB Date: Sat, 22 Nov 2025 15:45:09 +0000 Subject: [PATCH 07/11] Fix repeat function to throw an error for negative counts instead of returning a message --- Sprint-3/2-practice-tdd/repeat.js | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/Sprint-3/2-practice-tdd/repeat.js b/Sprint-3/2-practice-tdd/repeat.js index 685e3f3cd..84db1bf9f 100644 --- a/Sprint-3/2-practice-tdd/repeat.js +++ b/Sprint-3/2-practice-tdd/repeat.js @@ -1,6 +1,6 @@ function repeat(str , count) { if (count < 0) { - return "Count must be a non-negative integer"; + throw new Error("Count must be a non-negative integer"); } if (count === 0) { return ""; @@ -12,5 +12,4 @@ function repeat(str , count) { console.log(repeat("hello", -2)); - module.exports = repeat; From bbc996cad5af062922cd7b75338e495ae7a61d61 Mon Sep 17 00:00:00 2001 From: Payman IB Date: Sun, 23 Nov 2025 13:37:57 +0000 Subject: [PATCH 08/11] Refactor repeat function to correctly handle string repetition and maintain error handling for negative counts --- Sprint-3/2-practice-tdd/repeat.js | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/Sprint-3/2-practice-tdd/repeat.js b/Sprint-3/2-practice-tdd/repeat.js index 84db1bf9f..98a7a117c 100644 --- a/Sprint-3/2-practice-tdd/repeat.js +++ b/Sprint-3/2-practice-tdd/repeat.js @@ -4,12 +4,16 @@ function repeat(str , count) { } if (count === 0) { return ""; - } - for (let i = 0; i < count; i++) { - return str.repeat(count); - } + } + + let result = ""; + for (let i = 0; i < count; i++) { + result += str; + } + + return result; } - console.log(repeat("hello", -2)); + console.log(repeat("hello", 2)); module.exports = repeat; From 5ee3f6b012db382f8a0a209583c42df1d300b033 Mon Sep 17 00:00:00 2001 From: Payman IB Date: Sun, 23 Nov 2025 13:40:17 +0000 Subject: [PATCH 09/11] Update negative count test to ensure it throws an error instead of returning a message --- Sprint-3/2-practice-tdd/repeat.test.js | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/Sprint-3/2-practice-tdd/repeat.test.js b/Sprint-3/2-practice-tdd/repeat.test.js index 8d9611f6a..53fe34b0d 100644 --- a/Sprint-3/2-practice-tdd/repeat.test.js +++ b/Sprint-3/2-practice-tdd/repeat.test.js @@ -36,9 +36,9 @@ test("should return an empty string when count is 0", () => { // 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 return a message for negative count", () => { +test("should throw an error for negative count", () => { const str = "error"; const count = -2; - const repeatedStr = repeat(str, count); - expect(repeatedStr).toEqual("Count must be a non-negative integer"); + + expect(() => repeat(str, count)).toThrow("Count must be a non-negative integer"); }); \ No newline at end of file From 354b5aca5e8211f961582167642b85cf78344494 Mon Sep 17 00:00:00 2001 From: Payman IB Date: Sun, 23 Nov 2025 15:14:52 +0000 Subject: [PATCH 10/11] Refactor getOrdinalNumber function for clarity and improve test cases for better coverage --- Sprint-3/2-practice-tdd/get-ordinal-number.js | 6 +++--- Sprint-3/2-practice-tdd/get-ordinal-number.test.js | 9 +++++++-- 2 files changed, 10 insertions(+), 5 deletions(-) diff --git a/Sprint-3/2-practice-tdd/get-ordinal-number.js b/Sprint-3/2-practice-tdd/get-ordinal-number.js index e6b2c2098..41e2cce6c 100644 --- a/Sprint-3/2-practice-tdd/get-ordinal-number.js +++ b/Sprint-3/2-practice-tdd/get-ordinal-number.js @@ -1,11 +1,11 @@ function getOrdinalNumber(num) { const suffixes = ["th", "st", "nd", "rd"]; - const date = num % 100; + const lastTwoDigits = num % 100; - if (date >= 11 && date <= 13) { + if (lastTwoDigits >= 11 && lastTwoDigits <= 13) { return num + "th"; } - const suffix = suffixes[(date % 10)] || "th"; + const suffix = suffixes[(lastTwoDigits % 10)] || "th"; return num + suffix; } 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 db284920d..397ed3ff9 100644 --- a/Sprint-3/2-practice-tdd/get-ordinal-number.test.js +++ b/Sprint-3/2-practice-tdd/get-ordinal-number.test.js @@ -24,11 +24,16 @@ test("append 'rd' to numbers ending in 3, except those ending in 13", () => { expect( getOrdinalNumber(33) ).toEqual("33rd"); expect( getOrdinalNumber(133) ).toEqual("133rd"); }); -test("append 'th' to numbers ending in 4, except those ending in 14", () => { +test("append 'th' to numbers ending in 4", () => { expect(getOrdinalNumber(4)).toEqual("4th"); expect( getOrdinalNumber(24) ).toEqual("24th"); expect( getOrdinalNumber(134) ).toEqual("134th"); }); test("should return '11th' for 11", () => { expect(getOrdinalNumber(11)).toEqual("11th"); -}); \ No newline at end of file +}); +test("append 'th' to numbers which are not ending in 1, 2, 3", () => { + expect(getOrdinalNumber(5)).toEqual("5th"); + expect( getOrdinalNumber(29) ).toEqual("29th"); + expect( getOrdinalNumber(138) ).toEqual("138th"); +}); \ No newline at end of file From fdc1b1a23b0e8d5f6ad036b9122f6e26d4f767a2 Mon Sep 17 00:00:00 2001 From: Payman IB Date: Sun, 23 Nov 2025 15:41:01 +0000 Subject: [PATCH 11/11] Fix test case description for numbers ending in 1 and remove redundant tests for 4 and 11 --- Sprint-3/2-practice-tdd/get-ordinal-number.test.js | 12 ++---------- 1 file changed, 2 insertions(+), 10 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 397ed3ff9..a20233ffb 100644 --- a/Sprint-3/2-practice-tdd/get-ordinal-number.test.js +++ b/Sprint-3/2-practice-tdd/get-ordinal-number.test.js @@ -8,7 +8,7 @@ const getOrdinalNumber = require("./get-ordinal-number"); // When the number is 1, // Then the function should return "1st" -test("append 'nd' to 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(131) ).toEqual("131st"); @@ -24,16 +24,8 @@ test("append 'rd' to numbers ending in 3, except those ending in 13", () => { expect( getOrdinalNumber(33) ).toEqual("33rd"); expect( getOrdinalNumber(133) ).toEqual("133rd"); }); -test("append 'th' to numbers ending in 4", () => { - expect(getOrdinalNumber(4)).toEqual("4th"); - expect( getOrdinalNumber(24) ).toEqual("24th"); - expect( getOrdinalNumber(134) ).toEqual("134th"); -}); -test("should return '11th' for 11", () => { - expect(getOrdinalNumber(11)).toEqual("11th"); -}); test("append 'th' to numbers which are not ending in 1, 2, 3", () => { expect(getOrdinalNumber(5)).toEqual("5th"); - expect( getOrdinalNumber(29) ).toEqual("29th"); + expect( getOrdinalNumber(24) ).toEqual("24th"); expect( getOrdinalNumber(138) ).toEqual("138th"); }); \ No newline at end of file