From d6a54b881db1a0749e7f282537fff0f2f3cc739a Mon Sep 17 00:00:00 2001 From: MehrozMunir Date: Tue, 3 Mar 2026 16:48:42 +0000 Subject: [PATCH 1/4] 2-practice-tdd completed --- Sprint-3/2-practice-tdd/count.js | 7 +- Sprint-3/2-practice-tdd/count.test.js | 78 ++++++++++++++++++- Sprint-3/2-practice-tdd/get-ordinal-number.js | 16 +++- .../2-practice-tdd/get-ordinal-number.test.js | 48 ++++++++++++ Sprint-3/2-practice-tdd/repeat-str.js | 18 ++++- Sprint-3/2-practice-tdd/repeat-str.test.js | 18 +++++ 6 files changed, 176 insertions(+), 9 deletions(-) diff --git a/Sprint-3/2-practice-tdd/count.js b/Sprint-3/2-practice-tdd/count.js index 95b6ebb7d4..c15d248ec5 100644 --- a/Sprint-3/2-practice-tdd/count.js +++ b/Sprint-3/2-practice-tdd/count.js @@ -1,5 +1,8 @@ function countChar(stringOfCharacters, findCharacter) { - return 5 + if (findCharacter === "") + return 0; + else + return stringOfCharacters.split(findCharacter).length -1; } -module.exports = countChar; +module.exports = countChar; \ No newline at end of file diff --git a/Sprint-3/2-practice-tdd/count.test.js b/Sprint-3/2-practice-tdd/count.test.js index 179ea0ddf7..b29875704f 100644 --- a/Sprint-3/2-practice-tdd/count.test.js +++ b/Sprint-3/2-practice-tdd/count.test.js @@ -4,7 +4,7 @@ const countChar = require("./count"); // When the countChar function is called with these inputs, // Then it should: -// Scenario: Multiple Occurrences +// Scenario 1: Multiple Occurrences // Given the input string `str`, // And a character `char` that occurs one or more times in `str` (e.g., 'a' in 'aaaaa'), // When the function is called with these inputs, @@ -17,8 +17,82 @@ test("should count multiple occurrences of a character", () => { expect(count).toEqual(5); }); -// Scenario: No Occurrences +// Scenario 2: No Occurrences // Given the input string `str`, // And a character `char` that does not exist within `str`. // When the function is called with these inputs, // Then it should return 0, indicating that no occurrences of `char` were found. + +test("should return 0 if there are no occurrences of a character", () => { + const str = "there is no two number here"; + const char = "2"; + const count = countChar(str, char); + expect(count).toEqual(0); +}); + + +// Scenario 3: Single Occurrence +// Given the input string `str`, +// And a character `char` that exists only once within `str`. +// When the function is called with these inputs, +// Then it should return 1, indicating that single occurrence of `char` were found. + +test("should return 1 if there is single occurrence of a character", () => { + const str = " let's find if we got what you are looking for"; + const char = "'"; + const count = countChar(str, char); + expect(count).toEqual(1); +}); + +// Scenario 4: Zero occurrence when the string is empty +// Given the input string `str` which is empty, +// And a character `char` that can't be existed in the `str` as it is empty. +// When the function is called with these inputs, +// Then it should return 0, indicating that empty string can't hold that char. + +test("should return 0 as the string is empty", () => { + const str = ""; + const char = "2"; + const count = countChar(str, char); + expect(count).toEqual(0); +}); + +// Scenario 5: Multiple occurrences if the string contains single empty spaces and our char is an empty space +// Given the input string `str` that contains ' ' empty spaces, +// And a character `char` which is just single empty space ' '. +// When the function is called with these inputs, +// Then it should return the number of occurrences of single empty spaces in the string. + +test("should return multiple occurrences of empty spaces in the string", () => { + const str = "Hi I have got ten single empty spaces in this string."; + const char = " "; + const count = countChar(str, char); + expect(count).toEqual(10); +}); + + +// Scenario 6: No occurrences if the char is just an empty char +// Given the input string `str` that contains any number of characters or no characters at all, +// And a character `char` which is empty ''. +// When the function is called with these inputs, +// Then it should return 0 as there is nothing to count as the char is just empty. + +test("should return 0 as the char is just empty", () => { + const str = "Hi I am a string that can be empty or not empty but I can't count empty characters."; + const char = ""; + const count = countChar(str, char); + expect(count).toEqual(0); +}); + +// Scenario 7: Multiple Occurrences at different places +// Given the input string `str`, +// And a character `char` that occurs more than one times in `str` (e.g., 'a' in 'add the angle to the antenna'), +// When the function is called with these inputs, +// Then it should correctly count occurrences of `char` a in the string. + +test("should count multiple occurrences of a character", () => { + const str = "add the angle to the antenna"; + const char = "a"; + const count = countChar(str, char); + expect(count).toEqual(4); +}); \ No newline at end of file diff --git a/Sprint-3/2-practice-tdd/get-ordinal-number.js b/Sprint-3/2-practice-tdd/get-ordinal-number.js index f95d71db13..7e37a5db4e 100644 --- a/Sprint-3/2-practice-tdd/get-ordinal-number.js +++ b/Sprint-3/2-practice-tdd/get-ordinal-number.js @@ -1,5 +1,17 @@ function getOrdinalNumber(num) { - return "1st"; + let numberString = String(num); + lastTwoCharactersNumberString = numberString.slice(-2); + lastCharacterNumberString = numberString.slice(-1); + if(lastTwoCharactersNumberString === "11") + return num+ "th"; + else if(lastCharacterNumberString === "1") + return num+ "st"; + else if(lastCharacterNumberString === "2") + return num+"nd"; + else if(lastCharacterNumberString === "3") + return num + "rd"; + else + return num + "th"; } -module.exports = getOrdinalNumber; +module.exports = getOrdinalNumber; \ No newline at end of file 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 adfa58560f..af2788a671 100644 --- a/Sprint-3/2-practice-tdd/get-ordinal-number.test.js +++ b/Sprint-3/2-practice-tdd/get-ordinal-number.test.js @@ -17,4 +17,52 @@ test("should append 'st' for numbers ending with 1, except those ending with 11" expect(getOrdinalNumber(1)).toEqual("1st"); expect(getOrdinalNumber(21)).toEqual("21st"); expect(getOrdinalNumber(131)).toEqual("131st"); + expect(getOrdinalNumber(-171)).toEqual("-171st"); }); + +// Case 2: Special case for number 11) +// When the number is 11 or the numbers ends with 11, +// Then the function should return a string by appending "th" to the number. +test("should append 'th' for number 11", () => { + expect(getOrdinalNumber(11)).toEqual("11th"); + expect(getOrdinalNumber(111)).toEqual("111th"); + expect(getOrdinalNumber(12211)).toEqual("12211th"); + expect(getOrdinalNumber(98011)).toEqual("98011th"); + expect(getOrdinalNumber(-87611)).toEqual("-87611th"); +}); + +// Case 3: Numbers ending with 2 +// When the number is ending with 2, +// Then the function should return a string by appending "nd" to the number. +test("should append 'st' for numbers ending with 2", () => { + expect(getOrdinalNumber(2)).toEqual("2nd"); + expect(getOrdinalNumber(22)).toEqual("22nd"); + expect(getOrdinalNumber(152)).toEqual("152nd"); + expect(getOrdinalNumber(-82)).toEqual("-82nd"); +}); + + +// Case 4: Numbers ending with 3 +// When the number is ending with 3, +// Then the function should return a string by appending "rd" to the number. +test("should append 'st' for numbers ending with 3", () => { + expect(getOrdinalNumber(3)).toEqual("3rd"); + expect(getOrdinalNumber(33)).toEqual("33rd"); + expect(getOrdinalNumber(183)).toEqual("183rd"); + expect(getOrdinalNumber(-903)).toEqual("-903rd"); +}); + +// Case 5: Numbers not ending 1, 2 and 3 except 11 +// When the number is not ending with 1, 2 and 3 except 11, +// Then the function should return a string by appending "th" to the number. +test("should append 'th' for numbers not ending with 1, 2 and 3 except those ending with 11", () => { + expect(getOrdinalNumber(4)).toEqual("4th"); + expect(getOrdinalNumber(10)).toEqual("10th"); + expect(getOrdinalNumber(0)).toEqual("0th"); + expect(getOrdinalNumber(125)).toEqual("125th"); + expect(getOrdinalNumber(1000)).toEqual("1000th"); + expect(getOrdinalNumber(87939)).toEqual("87939th"); + expect(getOrdinalNumber(-780987)).toEqual("-780987th"); + +}); + diff --git a/Sprint-3/2-practice-tdd/repeat-str.js b/Sprint-3/2-practice-tdd/repeat-str.js index 3838c7b003..ed91c89573 100644 --- a/Sprint-3/2-practice-tdd/repeat-str.js +++ b/Sprint-3/2-practice-tdd/repeat-str.js @@ -1,5 +1,17 @@ -function repeatStr() { - return "hellohellohello"; +function repeatStr(str, count) { + try{ + if(count === 0) + return ""; + else if(count === 1) + return str; + else if(count < 0) + throw new Error("an error is thrown"); + else + return str.repeat(count); + } + catch(e){ + return e.message; + } } -module.exports = repeatStr; +module.exports = repeatStr; \ No newline at end of file diff --git a/Sprint-3/2-practice-tdd/repeat-str.test.js b/Sprint-3/2-practice-tdd/repeat-str.test.js index a3fc1196c4..5cf798e687 100644 --- a/Sprint-3/2-practice-tdd/repeat-str.test.js +++ b/Sprint-3/2-practice-tdd/repeat-str.test.js @@ -20,13 +20,31 @@ test("should repeat the string count times", () => { // 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. +test("should repeat the original string as the count is 1", () => { + const str = "OneWord"; + const count = 1; + const repeatedStr = repeatStr(str, count); + expect(repeatedStr).toEqual("OneWord"); +}); // Case: 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. +test("should return an empty string as the count is 0", () => { + const str = "hello"; + const count = 0; + const repeatedStr = repeatStr(str, count); + expect(repeatedStr).toEqual(""); +}); // Case: Handle 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, as negative counts are not valid. +test("should repeat the string count times", () => { + const str = "hello"; + const count = -2; + const repeatedStr = repeatStr(str, count); + expect(repeatedStr).toEqual("an error is thrown"); +}); \ No newline at end of file From 6936466cf3d46980f0a8e1226bd09750743950e5 Mon Sep 17 00:00:00 2001 From: MehrozMunir Date: Sat, 14 Mar 2026 14:53:30 +0000 Subject: [PATCH 2/4] worked on the feedback --- Sprint-3/2-practice-tdd/count.js | 8 +++----- Sprint-3/2-practice-tdd/count.test.js | 7 +++---- Sprint-3/2-practice-tdd/get-ordinal-number.js | 17 ++++++----------- .../2-practice-tdd/get-ordinal-number.test.js | 5 +---- Sprint-3/2-practice-tdd/repeat-str.js | 19 +++++++------------ Sprint-3/2-practice-tdd/repeat-str.test.js | 2 +- 6 files changed, 21 insertions(+), 37 deletions(-) diff --git a/Sprint-3/2-practice-tdd/count.js b/Sprint-3/2-practice-tdd/count.js index c15d248ec5..8f13eba629 100644 --- a/Sprint-3/2-practice-tdd/count.js +++ b/Sprint-3/2-practice-tdd/count.js @@ -1,8 +1,6 @@ function countChar(stringOfCharacters, findCharacter) { - if (findCharacter === "") - return 0; - else - return stringOfCharacters.split(findCharacter).length -1; + if (findCharacter.length === 0) return 0; + else return stringOfCharacters.split(findCharacter).length - 1; } -module.exports = countChar; \ No newline at end of file +module.exports = countChar; diff --git a/Sprint-3/2-practice-tdd/count.test.js b/Sprint-3/2-practice-tdd/count.test.js index b29875704f..4aefd4c6dc 100644 --- a/Sprint-3/2-practice-tdd/count.test.js +++ b/Sprint-3/2-practice-tdd/count.test.js @@ -30,7 +30,6 @@ test("should return 0 if there are no occurrences of a character", () => { expect(count).toEqual(0); }); - // Scenario 3: Single Occurrence // Given the input string `str`, // And a character `char` that exists only once within `str`. @@ -70,7 +69,6 @@ test("should return multiple occurrences of empty spaces in the string", () => { expect(count).toEqual(10); }); - // Scenario 6: No occurrences if the char is just an empty char // Given the input string `str` that contains any number of characters or no characters at all, // And a character `char` which is empty ''. @@ -78,7 +76,8 @@ test("should return multiple occurrences of empty spaces in the string", () => { // Then it should return 0 as there is nothing to count as the char is just empty. test("should return 0 as the char is just empty", () => { - const str = "Hi I am a string that can be empty or not empty but I can't count empty characters."; + const str = + "Hi I am a string that can be empty or not empty but I can't count empty characters."; const char = ""; const count = countChar(str, char); expect(count).toEqual(0); @@ -95,4 +94,4 @@ test("should count multiple occurrences of a character", () => { const char = "a"; const count = countChar(str, char); expect(count).toEqual(4); -}); \ No newline at end of file +}); diff --git a/Sprint-3/2-practice-tdd/get-ordinal-number.js b/Sprint-3/2-practice-tdd/get-ordinal-number.js index 7e37a5db4e..43dd1a8f6a 100644 --- a/Sprint-3/2-practice-tdd/get-ordinal-number.js +++ b/Sprint-3/2-practice-tdd/get-ordinal-number.js @@ -2,16 +2,11 @@ function getOrdinalNumber(num) { let numberString = String(num); lastTwoCharactersNumberString = numberString.slice(-2); lastCharacterNumberString = numberString.slice(-1); - if(lastTwoCharactersNumberString === "11") - return num+ "th"; - else if(lastCharacterNumberString === "1") - return num+ "st"; - else if(lastCharacterNumberString === "2") - return num+"nd"; - else if(lastCharacterNumberString === "3") - return num + "rd"; - else - return num + "th"; + if (lastTwoCharactersNumberString === "11") return num + "th"; + else if (lastCharacterNumberString === "1") return num + "st"; + else if (lastCharacterNumberString === "2") return num + "nd"; + else if (lastCharacterNumberString === "3") return num + "rd"; + else return num + "th"; } -module.exports = getOrdinalNumber; \ No newline at end of file +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 af2788a671..550507051b 100644 --- a/Sprint-3/2-practice-tdd/get-ordinal-number.test.js +++ b/Sprint-3/2-practice-tdd/get-ordinal-number.test.js @@ -41,7 +41,6 @@ test("should append 'st' for numbers ending with 2", () => { expect(getOrdinalNumber(-82)).toEqual("-82nd"); }); - // Case 4: Numbers ending with 3 // When the number is ending with 3, // Then the function should return a string by appending "rd" to the number. @@ -49,7 +48,7 @@ test("should append 'st' for numbers ending with 3", () => { expect(getOrdinalNumber(3)).toEqual("3rd"); expect(getOrdinalNumber(33)).toEqual("33rd"); expect(getOrdinalNumber(183)).toEqual("183rd"); - expect(getOrdinalNumber(-903)).toEqual("-903rd"); + expect(getOrdinalNumber(-903)).toEqual("-903rd"); }); // Case 5: Numbers not ending 1, 2 and 3 except 11 @@ -63,6 +62,4 @@ test("should append 'th' for numbers not ending with 1, 2 and 3 except those end expect(getOrdinalNumber(1000)).toEqual("1000th"); expect(getOrdinalNumber(87939)).toEqual("87939th"); expect(getOrdinalNumber(-780987)).toEqual("-780987th"); - }); - diff --git a/Sprint-3/2-practice-tdd/repeat-str.js b/Sprint-3/2-practice-tdd/repeat-str.js index ed91c89573..39bf0b92db 100644 --- a/Sprint-3/2-practice-tdd/repeat-str.js +++ b/Sprint-3/2-practice-tdd/repeat-str.js @@ -1,17 +1,12 @@ function repeatStr(str, count) { - try{ - if(count === 0) - return ""; - else if(count === 1) - return str; - else if(count < 0) - throw new Error("an error is thrown"); - else - return str.repeat(count); - } - catch(e){ + try { + if (count === 0) return ""; + else if (count === 1) return str; + else if (count < 0) throw new Error("an error is thrown"); + else return str.repeat(count); + } catch (e) { return e.message; } } -module.exports = repeatStr; \ No newline at end of file +module.exports = repeatStr; diff --git a/Sprint-3/2-practice-tdd/repeat-str.test.js b/Sprint-3/2-practice-tdd/repeat-str.test.js index 5cf798e687..2be0e18835 100644 --- a/Sprint-3/2-practice-tdd/repeat-str.test.js +++ b/Sprint-3/2-practice-tdd/repeat-str.test.js @@ -47,4 +47,4 @@ test("should repeat the string count times", () => { const count = -2; const repeatedStr = repeatStr(str, count); expect(repeatedStr).toEqual("an error is thrown"); -}); \ No newline at end of file +}); From 0b70ab510531b899b230789f5e6a556be9fe5dbd Mon Sep 17 00:00:00 2001 From: MehrozMunir Date: Sat, 14 Mar 2026 15:16:58 +0000 Subject: [PATCH 3/4] error thrown issue is fixed --- Sprint-3/2-practice-tdd/repeat-str.js | 12 ++++-------- Sprint-3/2-practice-tdd/repeat-str.test.js | 7 ++++--- 2 files changed, 8 insertions(+), 11 deletions(-) diff --git a/Sprint-3/2-practice-tdd/repeat-str.js b/Sprint-3/2-practice-tdd/repeat-str.js index 39bf0b92db..5fe66ebc74 100644 --- a/Sprint-3/2-practice-tdd/repeat-str.js +++ b/Sprint-3/2-practice-tdd/repeat-str.js @@ -1,12 +1,8 @@ function repeatStr(str, count) { - try { - if (count === 0) return ""; - else if (count === 1) return str; - else if (count < 0) throw new Error("an error is thrown"); - else return str.repeat(count); - } catch (e) { - return e.message; - } + if (count === 0) return ""; + else if (count === 1) return str; + else if (count < 0) throw new Error("an error is thrown"); + else return str.repeat(count); } module.exports = repeatStr; diff --git a/Sprint-3/2-practice-tdd/repeat-str.test.js b/Sprint-3/2-practice-tdd/repeat-str.test.js index 2be0e18835..47b41d80cb 100644 --- a/Sprint-3/2-practice-tdd/repeat-str.test.js +++ b/Sprint-3/2-practice-tdd/repeat-str.test.js @@ -42,9 +42,10 @@ test("should return an empty string as the count is 0", () => { // 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, as negative counts are not valid. -test("should repeat the string count times", () => { +test("should throw an error when count is a negative number", () => { const str = "hello"; const count = -2; - const repeatedStr = repeatStr(str, count); - expect(repeatedStr).toEqual("an error is thrown"); + expect(() => { + repeatStr(str, count); + }).toThrow(); }); From 87eed81bf7f975f79172616d0bc073b1517bec8a Mon Sep 17 00:00:00 2001 From: MehrozMunir Date: Mon, 16 Mar 2026 14:49:13 +0000 Subject: [PATCH 4/4] refactoring of code after the feedback --- Sprint-3/2-practice-tdd/count.js | 2 +- Sprint-3/2-practice-tdd/repeat-str.js | 2 +- Sprint-3/2-practice-tdd/repeat-str.test.js | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/Sprint-3/2-practice-tdd/count.js b/Sprint-3/2-practice-tdd/count.js index 8f13eba629..e2060494da 100644 --- a/Sprint-3/2-practice-tdd/count.js +++ b/Sprint-3/2-practice-tdd/count.js @@ -1,5 +1,5 @@ function countChar(stringOfCharacters, findCharacter) { - if (findCharacter.length === 0) return 0; + if (findCharacter.length === 0 || stringOfCharacters.length === 0) return 0; else return stringOfCharacters.split(findCharacter).length - 1; } diff --git a/Sprint-3/2-practice-tdd/repeat-str.js b/Sprint-3/2-practice-tdd/repeat-str.js index 5fe66ebc74..ceb4afeb72 100644 --- a/Sprint-3/2-practice-tdd/repeat-str.js +++ b/Sprint-3/2-practice-tdd/repeat-str.js @@ -1,7 +1,7 @@ function repeatStr(str, count) { if (count === 0) return ""; else if (count === 1) return str; - else if (count < 0) throw new Error("an error is thrown"); + else if (count < 0) throw new Error("Count can't be negative"); else return str.repeat(count); } diff --git a/Sprint-3/2-practice-tdd/repeat-str.test.js b/Sprint-3/2-practice-tdd/repeat-str.test.js index 47b41d80cb..30b817798e 100644 --- a/Sprint-3/2-practice-tdd/repeat-str.test.js +++ b/Sprint-3/2-practice-tdd/repeat-str.test.js @@ -47,5 +47,5 @@ test("should throw an error when count is a negative number", () => { const count = -2; expect(() => { repeatStr(str, count); - }).toThrow(); + }).toThrow("Count can't be negative"); });