Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions Sprint-3/2-practice-tdd/count.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
function countChar(stringOfCharacters, findCharacter) {
return 5
if (findCharacter === "")
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

good validation checking. is there perhaps another parameter here that could result in the same output?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes I think that I can check the length of the findCharacter here and if that is 0 I would return 0;

if (findCharacter.length === 0) return 0;

And I think that is a better validation than checking if string is empty. I have replaced this in my code now.

Thanks

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

While that is true I was actually referring to something else.
Look at the method again and see is there perhaps another parameter here that could result in the same output?

Copy link
Copy Markdown
Author

@MehrozMunir MehrozMunir Mar 16, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

oh yes. Actually, if stringOfCharacters.length === 0, I am expecting the same result. I have added it to my code now.

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@MehrozMunir 100% yes! defensive coding is always a good idea

return 0;
else
return stringOfCharacters.split(findCharacter).length -1;
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

interesting use of split to solve this question, it works perfectly.
how did you think to use split?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When I was thinking about the solutions how can i count the number of characters in a given string. The first thing came to my mind was the use of some loop to go through each character of string and compare it with the character i am looking for and add 1 to your count.

But, then I realized that I can solve it more efficiently without using loop with the use of split method as through split I can get an array of strings separated by character i am looking for in the separater parameter.

So, if I have five occurences of my character in the string, it will return my an array of size 6 that would be strings separated by that character including the empty strings.

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Very good, nice innovative solution

}

module.exports = countChar;
module.exports = countChar;
78 changes: 76 additions & 2 deletions Sprint-3/2-practice-tdd/count.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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);
});
16 changes: 14 additions & 2 deletions Sprint-3/2-practice-tdd/get-ordinal-number.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,17 @@
function getOrdinalNumber(num) {
return "1st";
let numberString = String(num);
lastTwoCharactersNumberString = numberString.slice(-2);
lastCharacterNumberString = numberString.slice(-1);
if(lastTwoCharactersNumberString === "11")
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please check your formatting in this file.
make sure it follows the style guide and its consistent so that its easily readable and maintainable

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am sorry my mistake. I had installed prettier for formatting, but did not enable it in the settings as the default formatter and use it on save. I have done it now. And it looks much better.

Thank you.

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;
48 changes: 48 additions & 0 deletions Sprint-3/2-practice-tdd/get-ordinal-number.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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");

});

18 changes: 15 additions & 3 deletions Sprint-3/2-practice-tdd/repeat-str.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,17 @@
function repeatStr() {
return "hellohellohello";
function repeatStr(str, count) {
try{
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please check your formatting in this file.
make sure it follows the style guide and its consistent so that its easily readable and maintainable

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){
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why are you wrapping this in a try catch?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was wrapping it because i was catching the thrown error in the catch block and send the error message string back so that I will be able to compare this string in my test file and would know that the error actually is being thrown.

But, I just have learned about the better way how can I test errors thrown in my test file while I was working on a different part of this module. And for that I don't need try catch block here. I have implement this change in the code.

Thanks.

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Okay awesome.
Yes there are definitely use cases for "try catch" but its generally not good practice to do "try {} catch (Exception e)" as this treats all exceptions equally and will result in the same behaviour regardless of the type/severity of the exception. it is better to be more explicit with errors and know exactly how you are handling each error. More effort but very worth it when you are trying to debug an issue and all you see is "exception was thrown"

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, thank you for the advice. I totally agree with that. I have updated the error message now.

Thanks.

return e.message;
}
}

module.exports = repeatStr;
module.exports = repeatStr;
18 changes: 18 additions & 0 deletions Sprint-3/2-practice-tdd/repeat-str.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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");
});
Loading