From 6ab27fc3699952eb7a8627d7ddf40e3eedb4897d Mon Sep 17 00:00:00 2001 From: Fares Bakhet Date: Sun, 12 Oct 2025 21:10:43 +0100 Subject: [PATCH 01/12] Biuld a countChar Code --- Sprint-3/2-practice-tdd/count.js | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/Sprint-3/2-practice-tdd/count.js b/Sprint-3/2-practice-tdd/count.js index 95b6ebb7d..698900cc7 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 +function countChar(str, char) { + return str.split(char).length - 1; } module.exports = countChar; + +console.log( + countChar( + "The split() method of String values takes a pattern and divides this string into an ordered list of substrings by searching for the pattern, puts these substrings into an array, and returns the array.", + "a" + ) +); From 9c2b470e202120022658c1c29f655f9c04201531 Mon Sep 17 00:00:00 2001 From: Fares Bakhet Date: Sun, 12 Oct 2025 21:35:52 +0100 Subject: [PATCH 02/12] Pass the count code test --- Sprint-3/2-practice-tdd/count.test.js | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/Sprint-3/2-practice-tdd/count.test.js b/Sprint-3/2-practice-tdd/count.test.js index 42baf4b4b..bf48e6f9b 100644 --- a/Sprint-3/2-practice-tdd/count.test.js +++ b/Sprint-3/2-practice-tdd/count.test.js @@ -2,7 +2,7 @@ const countChar = require("./count"); // Given a string str and a single character char to search for, // When the countChar function is called with these inputs, -// Then it should: +// Then it should: Return how many time the char occurs in the str. // Scenario: Multiple Occurrences // Given the input string str, @@ -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 exist in the string", () => { + const str = "hello world"; + const char = "z"; + const count = countChar(str, char); + expect(count).toEqual(0); +}); From 48dba8f1ace9da3c9341f001b7ce9e5e474429fc Mon Sep 17 00:00:00 2001 From: Fares Bakhet Date: Sun, 12 Oct 2025 21:52:41 +0100 Subject: [PATCH 03/12] Build getOrdinalNumber code --- Sprint-3/2-practice-tdd/get-ordinal-number.js | 39 ++++++++++++++++++- 1 file changed, 38 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..ad3f79153 100644 --- a/Sprint-3/2-practice-tdd/get-ordinal-number.js +++ b/Sprint-3/2-practice-tdd/get-ordinal-number.js @@ -1,5 +1,42 @@ function getOrdinalNumber(num) { - return "1st"; + num = num.toString(); + if ( + num.slice(-2) === "11" || + num.slice(-2) === "12" || + num.slice(-2) === "13" + ) { + return num + "th"; + } else if (num.slice(-1) === "1") { + return num + "st"; + return num + "st"; + } else if (num.slice(-1) === "2") { + return num + "nd"; + } else if (num.slice(-1) === "3") { + return num + "rd"; + } else { + return num + "th"; + } } module.exports = getOrdinalNumber; + +console.log(getOrdinalNumber(1)); +console.log(getOrdinalNumber(2)); +console.log(getOrdinalNumber(3)); +console.log(getOrdinalNumber(4)); +console.log(getOrdinalNumber(5)); +console.log(getOrdinalNumber(6)); +console.log(getOrdinalNumber(7)); +console.log(getOrdinalNumber(8)); +console.log(getOrdinalNumber(9)); +console.log(getOrdinalNumber(10)); +console.log(getOrdinalNumber(11)); +console.log(getOrdinalNumber(12)); +console.log(getOrdinalNumber(13)); +console.log(getOrdinalNumber(21)); +console.log(getOrdinalNumber(22)); +console.log(getOrdinalNumber(23)); +console.log(getOrdinalNumber(101)); +console.log(getOrdinalNumber(111)); +console.log(getOrdinalNumber(112)); +console.log(getOrdinalNumber(113)); From 76de1cad36ee4d25a644f617bf54de9893dff798 Mon Sep 17 00:00:00 2001 From: Fares Bakhet Date: Sun, 12 Oct 2025 21:56:02 +0100 Subject: [PATCH 04/12] Pass getOrdinalNumber tests --- .../2-practice-tdd/get-ordinal-number.test.js | 80 +++++++++++++++++++ 1 file changed, 80 insertions(+) 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..77eed4da5 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,83 @@ 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 12 +// When the number is 12, +// Then the function should return "12th" + +test("should return '12th' for 12", () => { + expect(getOrdinalNumber(12)).toEqual("12th"); +}); + +// Case 7: Identify the ordinal number for 13 +// When the number is 13, +// Then the function should return "13th" + +test("should return '13th' for 13", () => { + expect(getOrdinalNumber(13)).toEqual("13th"); +}); + +// Case 8: 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 9: 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 10: 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"); +}); + +// Case 11: Identify the ordinal number for 101 +// When the number is 101, +// Then the function should return "101st" + +test("should return '101st' for 101", () => { + expect(getOrdinalNumber(101)).toEqual("101st"); +}); From 7323aba8a4d61a2e42828cc39a896a64fdc19a20 Mon Sep 17 00:00:00 2001 From: Fares Bakhet Date: Sun, 12 Oct 2025 22:19:34 +0100 Subject: [PATCH 05/12] Build a function repeat code --- Sprint-3/2-practice-tdd/repeat.js | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/Sprint-3/2-practice-tdd/repeat.js b/Sprint-3/2-practice-tdd/repeat.js index 00e60d7f3..81f80694a 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"; + const str = arguments[0]; + const count = arguments[1]; + if (count < 0) { + throw new Error("Count must be a non-negative integer"); + } + let result = ""; + for (let i = 0; i < count; i++) { + result += str; + } + return result; } module.exports = repeat; + +console.log(repeat("hello", 3)); From 80cde9a8ecd52df335fa6b37d30250fc649e15a9 Mon Sep 17 00:00:00 2001 From: Fares Bakhet Date: Sun, 12 Oct 2025 22:21:33 +0100 Subject: [PATCH 06/12] Pass the function repeat tests --- Sprint-3/2-practice-tdd/repeat.test.js | 21 ++++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) diff --git a/Sprint-3/2-practice-tdd/repeat.test.js b/Sprint-3/2-practice-tdd/repeat.test.js index 34097b09c..71f040aa0 100644 --- a/Sprint-3/2-practice-tdd/repeat.test.js +++ b/Sprint-3/2-practice-tdd/repeat.test.js @@ -2,7 +2,7 @@ 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: +// Then it should: repeat the str count times and return a new string containing the repeated str values. // case: repeat String: // Given a target string str and a positive integer count, @@ -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 when count is negative", () => { + const str = "hello"; + const count = -1; + expect(() => repeat(str, count)).toThrow( + "Count must be a non-negative integer" + ); +}); From bea3492f05bf8516881dd9edaa410ce3a0094db4 Mon Sep 17 00:00:00 2001 From: Fares Bakhet Date: Sat, 25 Oct 2025 11:04:49 +0100 Subject: [PATCH 07/12] Deleting the unreachable code and else form the last return --- Sprint-3/2-practice-tdd/get-ordinal-number.js | 4 +--- 1 file changed, 1 insertion(+), 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 ad3f79153..6fa76672b 100644 --- a/Sprint-3/2-practice-tdd/get-ordinal-number.js +++ b/Sprint-3/2-practice-tdd/get-ordinal-number.js @@ -8,14 +8,12 @@ function getOrdinalNumber(num) { return num + "th"; } else if (num.slice(-1) === "1") { return num + "st"; - return num + "st"; } else if (num.slice(-1) === "2") { return num + "nd"; } else if (num.slice(-1) === "3") { return num + "rd"; - } else { - return num + "th"; } + return num + "th"; } module.exports = getOrdinalNumber; From 44f3ca1ca71f3e8a4ea52f6b18c00009f6bec7f9 Mon Sep 17 00:00:00 2001 From: Fares Bakhet Date: Sat, 25 Oct 2025 14:25:02 +0100 Subject: [PATCH 08/12] Grouping the tests --- .../2-practice-tdd/get-ordinal-number.test.js | 89 +++---------------- 1 file changed, 13 insertions(+), 76 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 77eed4da5..596e16bd4 100644 --- a/Sprint-3/2-practice-tdd/get-ordinal-number.test.js +++ b/Sprint-3/2-practice-tdd/get-ordinal-number.test.js @@ -4,90 +4,27 @@ 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", () => { +test("append 'st' to numbers ending in 1,except those ending in 11", () => { 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 12 -// When the number is 12, -// Then the function should return "12th" - -test("should return '12th' for 12", () => { - expect(getOrdinalNumber(12)).toEqual("12th"); -}); - -// Case 7: Identify the ordinal number for 13 -// When the number is 13, -// Then the function should return "13th" - -test("should return '13th' for 13", () => { - expect(getOrdinalNumber(13)).toEqual("13th"); -}); - -// Case 8: 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"); + expect(getOrdinalNumber(101)).toEqual("101st"); }); -// Case 9: Identify the ordinal number for 22 -// When the number is 22, -// Then the function should return "22nd" - -test("should return '22nd' for 22", () => { +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"); }); -// Case 10: Identify the ordinal number for 23 -// When the number is 23, -// Then the function should return "23rd" - -test("should return '23rd' for 23", () => { +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"); }); -// Case 11: Identify the ordinal number for 101 -// When the number is 101, -// Then the function should return "101st" - -test("should return '101st' for 101", () => { - expect(getOrdinalNumber(101)).toEqual("101st"); +test("should return 'th' for the rest numbers", () => { + expect(getOrdinalNumber(10)).toEqual("10th"); + expect(getOrdinalNumber(11)).toEqual("11th"); + expect(getOrdinalNumber(34)).toEqual("34th"); + expect(getOrdinalNumber(134)).toEqual("134th"); }); From c2a674b842f6ff996465d20163ad490894c8ce92 Mon Sep 17 00:00:00 2001 From: Fares Bakhet Date: Sat, 25 Oct 2025 14:29:19 +0100 Subject: [PATCH 09/12] Editing the code --- Sprint-3/2-practice-tdd/repeat.js | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/Sprint-3/2-practice-tdd/repeat.js b/Sprint-3/2-practice-tdd/repeat.js index 81f80694a..8ac0d8928 100644 --- a/Sprint-3/2-practice-tdd/repeat.js +++ b/Sprint-3/2-practice-tdd/repeat.js @@ -1,6 +1,6 @@ -function repeat() { - const str = arguments[0]; - const count = arguments[1]; +function repeat(str, count) { + //const str = arguments[0]; + //const count = arguments[1]; if (count < 0) { throw new Error("Count must be a non-negative integer"); } From 7d3028f11e3f4e40e9172eff18ad35380654045d Mon Sep 17 00:00:00 2001 From: Fares Bakhet Date: Sat, 25 Oct 2025 14:51:09 +0100 Subject: [PATCH 10/12] Editing last test --- Sprint-3/2-practice-tdd/get-ordinal-number.test.js | 4 ++-- 1 file changed, 2 insertions(+), 2 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 596e16bd4..a33cba2c0 100644 --- a/Sprint-3/2-practice-tdd/get-ordinal-number.test.js +++ b/Sprint-3/2-practice-tdd/get-ordinal-number.test.js @@ -22,9 +22,9 @@ test("append 'rd' to numbers ending in 3, except those ending in 13", () => { expect(getOrdinalNumber(133)).toEqual("133rd"); }); -test("should return 'th' for the rest numbers", () => { +test("should return 'th' for numbers where the last digit is not 1, 2, or 3, excluding special cases 11–13", () => { expect(getOrdinalNumber(10)).toEqual("10th"); expect(getOrdinalNumber(11)).toEqual("11th"); - expect(getOrdinalNumber(34)).toEqual("34th"); + expect(getOrdinalNumber(13)).toEqual("13th"); expect(getOrdinalNumber(134)).toEqual("134th"); }); From 1590eeca9e42fdc7c8d8ef6b4b12e2ae3dd7b660 Mon Sep 17 00:00:00 2001 From: Fares Bakhet Date: Sat, 25 Oct 2025 14:53:08 +0100 Subject: [PATCH 11/12] Delete unsued code --- Sprint-3/2-practice-tdd/repeat.js | 2 -- 1 file changed, 2 deletions(-) diff --git a/Sprint-3/2-practice-tdd/repeat.js b/Sprint-3/2-practice-tdd/repeat.js index 8ac0d8928..a8257d151 100644 --- a/Sprint-3/2-practice-tdd/repeat.js +++ b/Sprint-3/2-practice-tdd/repeat.js @@ -1,6 +1,4 @@ function repeat(str, count) { - //const str = arguments[0]; - //const count = arguments[1]; if (count < 0) { throw new Error("Count must be a non-negative integer"); } From 2478386b38940e45b07f1b31ca5533f0cbd89db8 Mon Sep 17 00:00:00 2001 From: Fares Bakhet Date: Sat, 25 Oct 2025 17:13:46 +0100 Subject: [PATCH 12/12] Removing all the unnecessary --- Sprint-3/2-practice-tdd/get-ordinal-number.js | 9 ++++++--- 1 file changed, 6 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 6fa76672b..1e25cb4e8 100644 --- a/Sprint-3/2-practice-tdd/get-ordinal-number.js +++ b/Sprint-3/2-practice-tdd/get-ordinal-number.js @@ -6,11 +6,14 @@ function getOrdinalNumber(num) { num.slice(-2) === "13" ) { return num + "th"; - } else if (num.slice(-1) === "1") { + } + if (num.slice(-1) === "1") { return num + "st"; - } else if (num.slice(-1) === "2") { + } + if (num.slice(-1) === "2") { return num + "nd"; - } else if (num.slice(-1) === "3") { + } + if (num.slice(-1) === "3") { return num + "rd"; } return num + "th";