Skip to content
Closed
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
10 changes: 6 additions & 4 deletions Sprint-3/2-practice-tdd/get-ordinal-number.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
function getOrdinalNumber(num) {
let result;
if (num % 100 == 11 || num % 100 == 12 || num % 100 == 13){
let rem_100 = num % 100;
let rem_10 = num % 10;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Could consider naming them lastDigit and lastTwoDigits (more human readable).

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.

Thanks. I have changed it.

if (rem_100 == 11 || rem_100 == 12 || rem_100 == 13){
result = num.toString() + "th";
}
else if (num % 10 == 1){
else if (rem_10 == 1){
result = num.toString() +"st";
}
else if (num % 10 == 2){
else if (rem_10 == 2){
result = num.toString() + "nd";
}
else if (num % 10 == 3){
else if (rem_10 == 3){
result = num.toString() + "rd";
}
else {
Expand Down
5 changes: 5 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 @@ -34,3 +34,8 @@ test("should return '12th' for 12", () => {
test("should return '13th' for 13", () => {
expect(getOrdinalNumber(13)).toEqual("13th");
});
Comment on lines +16 to +45

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

To ensure thorough testing, we need broad scenarios that cover all possible cases.
Listing individual values, however, can quickly lead to an unmanageable number of test cases.
Instead of writing tests for individual numbers, consider grouping all possible input values into meaningful categories.
Then, select representative samples from each category to test. This approach improves coverage and makes our tests easier to maintain.

For example, we can prepare a test for numbers 2, 22, 132, etc. as

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");
});

Can you make the tests in this script more comprehensive?

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.

Thanks, I have add it.

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");
});

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

This is just an example. Why not update the whole test script accordingly to make the test more comprehensive?

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.

Thanks. I have added other tests.

8 changes: 4 additions & 4 deletions Sprint-3/2-practice-tdd/repeat.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
function repeat(str, count) {

if (count < 0){
return null
throw new Error("Count must be positive number!");
}
Comment on lines +3 to +5

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

This works.

An alternative is to throw an exception.

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.

Thanks for advice, I have changed it.

else if (count == 0){
return ""
return "";
}
let result = "";
for ( let i=0; i<count; i++){
result +=str
result +=str;
}
return result
return result;
}

module.exports = repeat;
5 changes: 1 addition & 4 deletions Sprint-3/2-practice-tdd/repeat.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,5 @@ test("handle Count of 0", () => {
// 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("Negative Count", () => {
const str = "hello";
const count = -1;
const repeatedStr = repeat(str, count);
expect(repeatedStr).toEqual(null);
expect(() => repeat("hello", -1)).toThrow("Count must be positive number!");
});
Loading