From 889f34ea7e3503e68b79180903c509c441aafe79 Mon Sep 17 00:00:00 2001 From: AdnaanA Date: Sat, 27 Sep 2025 10:22:03 +0100 Subject: [PATCH 01/11] created and tested jest-testcases --- Sprint-3/2-practice-tdd/count.js | 11 +++++-- Sprint-3/2-practice-tdd/count.test.js | 41 +++++++++++++++++++++++++++ 2 files changed, 50 insertions(+), 2 deletions(-) diff --git a/Sprint-3/2-practice-tdd/count.js b/Sprint-3/2-practice-tdd/count.js index 95b6ebb7d..961d707dd 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) { + if (!char || char.length !== 1) return 0; // ensure char is a single character + let count = 0; + for (let i = 0; i < str.length; i++) { + if (str[i] === char) { + 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..014aba203 100644 --- a/Sprint-3/2-practice-tdd/count.test.js +++ b/Sprint-3/2-practice-tdd/count.test.js @@ -22,3 +22,44 @@ 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 = "mnopqrst"; + const char = "z"; + const count = countChar(str, char); + expect(count).toEqual(0); +}); + +// we can add more test cases to make sure our function is working as expected +// Scenario: Case Sensitivity +// Given the input string str, +// And a character char that exists in str but with different casing (e.g., 'A' in 'aAaAa'), +// When the function is called with these inputs, +// Then it should treat char as case-sensitive and only count occurrences that match the exact casing (e.g., 'A' appears two times in 'aAaAa'). + +test("should treat character as case-sensitive", () => { + const str = "aAaAa"; + const char = "A"; + const count = countChar(str, char); + expect(count).toEqual(2); +}); + +// we can also add empty string test case +// Scenario: Empty String +// Given an empty input string str, +// And any character char, +// When the function is called with these inputs, +// Then it should return 0, indicating that no occurrences of char can be found in an empty str. + +test("should return 0 for empty string", () => { + const str = ""; + const char = "a"; + const count = countChar(str, char); + expect(count).toEqual(0); +}); + +// We can run this test file using the command `npx jest count.test.js` +// in the terminal. Making sure we are in the directory where this file is located. +// If we have Jest installed globally, you can simply run `jest count.test.js` +// instead. If you have added a test script to your package.json file, you can also run +// `npm test count.test.js` to execute the tests. \ No newline at end of file From c40ecef10cdbb4513db791ccea02accbe4ffa05a Mon Sep 17 00:00:00 2001 From: AdnaanA Date: Sat, 27 Sep 2025 12:36:06 +0100 Subject: [PATCH 02/11] getordinalnumber test cases have been created and tested to pass all --- Sprint-3/2-practice-tdd/get-ordinal-number.js | 19 +++++- .../2-practice-tdd/get-ordinal-number.test.js | 64 +++++++++++++++++++ 2 files changed, 82 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..7c9cafd4a 100644 --- a/Sprint-3/2-practice-tdd/get-ordinal-number.js +++ b/Sprint-3/2-practice-tdd/get-ordinal-number.js @@ -1,5 +1,22 @@ function getOrdinalNumber(num) { - return "1st"; + + if (typeof num !== "number" || !Number.isInteger(num) || num <= 0) { + return "Invalid input"; + } +if (num % 100 >= 11 && num % 100 <= 13) { + return num + "th"; + } + switch (num % 10) { + 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..367906db3 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,67 @@ 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 102 +// When the number is 102, +// Then the function should return "102nd" + +test("should return '102nd' for 102", () => { + expect(getOrdinalNumber(102)).toEqual("102nd"); +}); + +// Case 8: Identify the ordinal number for 111 +// When the number is 111, +// Then the function should return "111th" + +test("should return '111th' for 111", () => { + expect(getOrdinalNumber(111)).toEqual("111th"); +}); + +// case 9: identify the ordinal number for 211 +// when the number is 211 +// then the function should return "211th" + +test("should return '33rd' for 33", () => { + expect(getOrdinalNumber(33)).toEqual("33rd"); +}); \ No newline at end of file From f097ae0923d1b8848abd7770c3c6ecd5ff4653bc Mon Sep 17 00:00:00 2001 From: AdnaanA Date: Mon, 29 Sep 2025 09:51:39 +0100 Subject: [PATCH 03/11] created test-case for repeat and tested the cases with a pass --- Sprint-3/2-practice-tdd/repeat.js | 28 +++++++++++++++++++++++++- Sprint-3/2-practice-tdd/repeat.test.js | 23 +++++++++++++++++++++ 2 files changed, 50 insertions(+), 1 deletion(-) diff --git a/Sprint-3/2-practice-tdd/repeat.js b/Sprint-3/2-practice-tdd/repeat.js index 00e60d7f3..8adc5558e 100644 --- a/Sprint-3/2-practice-tdd/repeat.js +++ b/Sprint-3/2-practice-tdd/repeat.js @@ -1,5 +1,31 @@ function repeat() { - return "hellohellohello"; + if (arguments.length !== 2) { + throw new Error("Function requires exactly two arguments: str and count."); + } + + const [str, count] = arguments; + + if (typeof str !== "string") { + throw new Error("First argument must be a string."); + } + + if (typeof count !== "number" || !Number.isInteger(count) || count < 0) { + throw new Error("Second argument must be a non-negative integer."); + } + + if (count === 0) { + return ""; + } + + if (count === 1) { + return str; + } + + let result = ""; + 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..7d61098c1 100644 --- a/Sprint-3/2-practice-tdd/repeat.test.js +++ b/Sprint-3/2-practice-tdd/repeat.test.js @@ -21,12 +21,35 @@ test("should repeat the string count times", () => { // 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 throw an error for negative count", () => { + const str = "error"; + const count = -2; + expect(() => repeat(str, count)).toThrow( + "Second argument must be a non-negative integer." + ); +}); + From 09a628b2616c37a44271755097e875d8a474827c Mon Sep 17 00:00:00 2001 From: AdnaanA Date: Thu, 9 Oct 2025 10:05:33 +0100 Subject: [PATCH 04/11] made if statement more concise --- Sprint-3/2-practice-tdd/get-ordinal-number.js | 2 +- 1 file changed, 1 insertion(+), 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 7c9cafd4a..1e56a6d16 100644 --- a/Sprint-3/2-practice-tdd/get-ordinal-number.js +++ b/Sprint-3/2-practice-tdd/get-ordinal-number.js @@ -1,6 +1,6 @@ function getOrdinalNumber(num) { - if (typeof num !== "number" || !Number.isInteger(num) || num <= 0) { + if (!Number.isInteger(num) || num <= 0) { return "Invalid input"; } if (num % 100 >= 11 && num % 100 <= 13) { From a87356bc590e999b7f05ff88d35ba5168d304ec2 Mon Sep 17 00:00:00 2001 From: AdnaanA Date: Thu, 9 Oct 2025 10:10:14 +0100 Subject: [PATCH 05/11] indentation fixed --- Sprint-3/2-practice-tdd/get-ordinal-number.js | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/Sprint-3/2-practice-tdd/get-ordinal-number.js b/Sprint-3/2-practice-tdd/get-ordinal-number.js index 1e56a6d16..0938cd9fe 100644 --- a/Sprint-3/2-practice-tdd/get-ordinal-number.js +++ b/Sprint-3/2-practice-tdd/get-ordinal-number.js @@ -1,9 +1,8 @@ function getOrdinalNumber(num) { - if (!Number.isInteger(num) || num <= 0) { return "Invalid input"; } -if (num % 100 >= 11 && num % 100 <= 13) { + if (num % 100 >= 11 && num % 100 <= 13) { return num + "th"; } switch (num % 10) { From 8b8e22f4bdbb1537fe059d8b73b05755fa8921ec Mon Sep 17 00:00:00 2001 From: AdnaanA Date: Thu, 9 Oct 2025 10:30:04 +0100 Subject: [PATCH 06/11] categorised the test-cases --- .../2-practice-tdd/get-ordinal-number.test.js | 67 ++++++++----------- 1 file changed, 28 insertions(+), 39 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 367906db3..1881a564a 100644 --- a/Sprint-3/2-practice-tdd/get-ordinal-number.test.js +++ b/Sprint-3/2-practice-tdd/get-ordinal-number.test.js @@ -8,70 +8,59 @@ const getOrdinalNumber = require("./get-ordinal-number"); // When the number is 1, // Then the function should return "1st" -test("should return '1st' for 1", () => { +// category 1: ends with 1 (but not 11) -> "st" +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(101)).toEqual("101st"); }); // 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", () => { +// category 2: ends with 2 (but not 12) -> "nd" +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(102)).toEqual("102nd"); }); // 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", () => { +// category 3: ends with 3 (but not 13) -> "rd" +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(103)).toEqual("103rd"); }); // 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", () => { +// category 4: all other numbers -> "th" +test("append 'th' to all other numbers", () => { 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"); + expect(getOrdinalNumber(12)).toEqual("12th"); + expect(getOrdinalNumber(13)).toEqual("13th"); + expect(getOrdinalNumber(14)).toEqual("14th"); + expect(getOrdinalNumber(111)).toEqual("111th"); + expect(getOrdinalNumber(112)).toEqual("112th"); + expect(getOrdinalNumber(113)).toEqual("113th"); + expect(getOrdinalNumber(114)).toEqual("114th"); }); -// 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 102 -// When the number is 102, -// Then the function should return "102nd" - -test("should return '102nd' for 102", () => { - expect(getOrdinalNumber(102)).toEqual("102nd"); -}); - -// Case 8: Identify the ordinal number for 111 -// When the number is 111, -// Then the function should return "111th" - -test("should return '111th' for 111", () => { +// category 5: special case for 11, 12, 13 -> "th" +test("append 'th' to numbers ending in 11, 12, or 13", () => { + expect(getOrdinalNumber(11)).toEqual("11th"); + expect(getOrdinalNumber(12)).toEqual("12th"); + expect(getOrdinalNumber(13)).toEqual("13th"); expect(getOrdinalNumber(111)).toEqual("111th"); + expect(getOrdinalNumber(112)).toEqual("112th"); + expect(getOrdinalNumber(113)).toEqual("113th"); }); -// case 9: identify the ordinal number for 211 -// when the number is 211 -// then the function should return "211th" - -test("should return '33rd' for 33", () => { - expect(getOrdinalNumber(33)).toEqual("33rd"); -}); \ No newline at end of file From b31f116cc98203502d57cc7156945b50462c8720 Mon Sep 17 00:00:00 2001 From: AdnaanA Date: Thu, 9 Oct 2025 11:08:40 +0100 Subject: [PATCH 07/11] fixed parameter declaration --- Sprint-3/2-practice-tdd/repeat.js | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/Sprint-3/2-practice-tdd/repeat.js b/Sprint-3/2-practice-tdd/repeat.js index 8adc5558e..a648b93dc 100644 --- a/Sprint-3/2-practice-tdd/repeat.js +++ b/Sprint-3/2-practice-tdd/repeat.js @@ -1,10 +1,8 @@ -function repeat() { +function repeat(str, count) { if (arguments.length !== 2) { throw new Error("Function requires exactly two arguments: str and count."); } - const [str, count] = arguments; - if (typeof str !== "string") { throw new Error("First argument must be a string."); } @@ -28,4 +26,5 @@ function repeat() { return result; } + module.exports = repeat; From daf6efa10b2069fcd9affadfcecf98f20c7ee47b Mon Sep 17 00:00:00 2001 From: AdnaanA Date: Thu, 9 Oct 2025 11:15:57 +0100 Subject: [PATCH 08/11] fixed if statement to be more concise --- Sprint-3/2-practice-tdd/repeat.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Sprint-3/2-practice-tdd/repeat.js b/Sprint-3/2-practice-tdd/repeat.js index a648b93dc..6d908e0d7 100644 --- a/Sprint-3/2-practice-tdd/repeat.js +++ b/Sprint-3/2-practice-tdd/repeat.js @@ -7,7 +7,7 @@ function repeat(str, count) { throw new Error("First argument must be a string."); } - if (typeof count !== "number" || !Number.isInteger(count) || count < 0) { + if (!Number.isSafeInteger(count) || count < 0) { throw new Error("Second argument must be a non-negative integer."); } From c7bc5986bd9d280cff74260ef8b546cc77bc8d9d Mon Sep 17 00:00:00 2001 From: AdnaanA Date: Thu, 9 Oct 2025 14:21:35 +0100 Subject: [PATCH 09/11] removed cat:5 test cases --- Sprint-3/2-practice-tdd/get-ordinal-number.test.js | 10 +--------- 1 file changed, 1 insertion(+), 9 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 1881a564a..17ed4d2f8 100644 --- a/Sprint-3/2-practice-tdd/get-ordinal-number.test.js +++ b/Sprint-3/2-practice-tdd/get-ordinal-number.test.js @@ -54,13 +54,5 @@ test("append 'th' to all other numbers", () => { expect(getOrdinalNumber(114)).toEqual("114th"); }); -// category 5: special case for 11, 12, 13 -> "th" -test("append 'th' to numbers ending in 11, 12, or 13", () => { - expect(getOrdinalNumber(11)).toEqual("11th"); - expect(getOrdinalNumber(12)).toEqual("12th"); - expect(getOrdinalNumber(13)).toEqual("13th"); - expect(getOrdinalNumber(111)).toEqual("111th"); - expect(getOrdinalNumber(112)).toEqual("112th"); - expect(getOrdinalNumber(113)).toEqual("113th"); -}); + From 4183f3ba605dd1af99ed3459745a8162f3e5e1d9 Mon Sep 17 00:00:00 2001 From: AdnaanA Date: Thu, 9 Oct 2025 14:26:08 +0100 Subject: [PATCH 10/11] fixed description for category 4 --- Sprint-3/2-practice-tdd/get-ordinal-number.test.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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 17ed4d2f8..c9d4aee0a 100644 --- a/Sprint-3/2-practice-tdd/get-ordinal-number.test.js +++ b/Sprint-3/2-practice-tdd/get-ordinal-number.test.js @@ -42,7 +42,7 @@ test("append 'rd' to numbers ending in 3, except those ending in 13", () => { // Then the function should return "4th" // category 4: all other numbers -> "th" -test("append 'th' to all other numbers", () => { +test("append 'th' to numbers ending in 0, 4–9, or 11–13", () => { expect(getOrdinalNumber(4)).toEqual("4th"); expect(getOrdinalNumber(11)).toEqual("11th"); expect(getOrdinalNumber(12)).toEqual("12th"); From c3eae644074cd139ce31b68385fafe8d0310bf47 Mon Sep 17 00:00:00 2001 From: AdnaanA Date: Thu, 9 Oct 2025 14:28:22 +0100 Subject: [PATCH 11/11] modified: Sprint-3/2-practice-tdd/get-ordinal-number.test.js --- Sprint-3/2-practice-tdd/get-ordinal-number.test.js | 1 + 1 file changed, 1 insertion(+) 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 c9d4aee0a..c22dcb0b4 100644 --- a/Sprint-3/2-practice-tdd/get-ordinal-number.test.js +++ b/Sprint-3/2-practice-tdd/get-ordinal-number.test.js @@ -44,6 +44,7 @@ test("append 'rd' to numbers ending in 3, except those ending in 13", () => { // category 4: all other numbers -> "th" test("append 'th' to numbers ending in 0, 4–9, or 11–13", () => { expect(getOrdinalNumber(4)).toEqual("4th"); + expect(getOrdinalNumber(10)).toEqual("10th"); expect(getOrdinalNumber(11)).toEqual("11th"); expect(getOrdinalNumber(12)).toEqual("12th"); expect(getOrdinalNumber(13)).toEqual("13th");