From d6a54b881db1a0749e7f282537fff0f2f3cc739a Mon Sep 17 00:00:00 2001 From: MehrozMunir Date: Tue, 3 Mar 2026 16:48:42 +0000 Subject: [PATCH 1/2] 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 62a32bd7cf4e8437e5e997abf9f3b58887e213a6 Mon Sep 17 00:00:00 2001 From: MehrozMunir Date: Thu, 5 Mar 2026 14:07:51 +0000 Subject: [PATCH 2/2] fixed dead code --- Sprint-3/3-dead-code/exercise-1.js | 3 +-- Sprint-3/3-dead-code/exercise-2.js | 4 ---- 2 files changed, 1 insertion(+), 6 deletions(-) diff --git a/Sprint-3/3-dead-code/exercise-1.js b/Sprint-3/3-dead-code/exercise-1.js index 4d09f15fa9..307c2530d9 100644 --- a/Sprint-3/3-dead-code/exercise-1.js +++ b/Sprint-3/3-dead-code/exercise-1.js @@ -1,7 +1,6 @@ // Find the instances of unreachable and redundant code - remove them! // The sayHello function should continue to work for any reasonable input it's given. -let testName = "Jerry"; const greeting = "hello"; function sayHello(greeting, name) { @@ -10,7 +9,7 @@ function sayHello(greeting, name) { console.log(greetingStr); } -testName = "Aman"; +const testName = "Aman"; const greetingMessage = sayHello(greeting, testName); diff --git a/Sprint-3/3-dead-code/exercise-2.js b/Sprint-3/3-dead-code/exercise-2.js index 56d7887c4c..6cd58f1dd7 100644 --- a/Sprint-3/3-dead-code/exercise-2.js +++ b/Sprint-3/3-dead-code/exercise-2.js @@ -5,10 +5,6 @@ const pets = ["parrot", "hamster", "horse", "dog", "hamster", "cat", "hamster"]; const capitalisedPets = pets.map((pet) => pet.toUpperCase()); const petsStartingWithH = pets.filter((pet) => pet[0] === "h"); -function logPets(petsArr) { - petsArr.forEach((pet) => console.log(pet)); -} - function countAndCapitalisePets(petsArr) { const petCount = {};