-
-
Notifications
You must be signed in to change notification settings - Fork 239
Birmingham | 25-ITP-Sep | Hadi-Vahidi| Sprint 3 |Coursework/sprint-3-practice-tdd #830
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Birmingham | 25-ITP-Sep | Hadi-Vahidi| Sprint 3 |Coursework/sprint-3-practice-tdd #830
Conversation
| const twoLastDig = num % 100; | ||
| if (twoLastDig === 11 || twoLastDig === 12 || twoLastDig === 13) { | ||
| return num + "th"; | ||
| } | ||
|
|
||
| const lastDig = num % 10; | ||
| if (lastDig === 1) return num + "st"; | ||
| if (lastDig === 2) return num + "nd"; | ||
| if (lastDig === 3) return num + "rd"; | ||
| return num + "th"; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Keeping it or its in the variable names would probably make them more meaningful.
| test("should return '11th' for 11", () => { | ||
| expect(getOrdinalNumber(11)).toEqual("11th"); | ||
| }); | ||
|
|
||
| test("should return '2nd' for 2", () => { | ||
| expect(getOrdinalNumber(2)).toEqual("2nd"); | ||
| }); | ||
|
|
||
|
|
||
| test("should return '23rd' for 23rd", () => { | ||
| expect(getOrdinalNumber(23)).toEqual("23rd"); | ||
| }); | ||
| test("should return '3rd' for 3", () => { | ||
| expect(getOrdinalNumber(3)).toEqual("3rd"); | ||
| }); | ||
| test("should return '4th' for 4", () => { | ||
| expect(getOrdinalNumber(4)).toEqual("4th"); | ||
| }); | ||
|
|
||
| test("should return '12th' for 12", () => { | ||
| expect(getOrdinalNumber(12)).toEqual("12th"); | ||
| }); | ||
| test("should return '13th' for 13", () => { | ||
| expect(getOrdinalNumber(13)).toEqual("13th"); | ||
| }); | ||
|
|
||
|
|
||
| test("should return '101st' for 101", () => { | ||
| expect(getOrdinalNumber(101)).toEqual("101st"); | ||
| }); |
There was a problem hiding this comment.
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");
});
| if(count<0){ | ||
| return "invalid" | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
How would the caller distinguish the result of the following two function calls?
repeat("invalid", 1)repeat("", -1)
| test("should repeat the string count times", () => { | ||
| const str = "hello"; | ||
| const count = -1; | ||
| const repeatedStr = repeat(str, count); | ||
| expect(repeatedStr).toEqual("invalid"); | ||
| }); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If you modified repeat() to throw an error when count is negative, and you wanted to test if the function can throw an error as expected, you can use .toThrow(). You can find out more about how to use .toThrow() here: https://jestjs.io/docs/expect#tothrowerror (Note: Pay close attention to the syntax of the example)
Learners, PR Template
Self checklist
Changelist
all tasks are done thank you very much
Questions