London | 25-ITP-Sep | Adnaan Abo | Sprint 3 | coursework/sprint-3-practice-tdd #718
London | 25-ITP-Sep | Adnaan Abo | Sprint 3 | coursework/sprint-3-practice-tdd #718AdnaanA wants to merge 11 commits intoCodeYourFuture:mainfrom
Conversation
cjyuan
left a comment
There was a problem hiding this comment.
There are also some exercises in Sprint-3/1-implement-and-rewrite-tests/.
| function getOrdinalNumber(num) { | ||
| return "1st"; | ||
|
|
||
| if (typeof num !== "number" || !Number.isInteger(num) || num <= 0) { |
There was a problem hiding this comment.
Can you think of a more concise way to express the condition of this if statement?
There was a problem hiding this comment.
Hello,
Thank you for the feedback.
Yes the If statement can be made more concise. Here’s a more compact version:
if (!Number.isInteger(num) || num <= 0) {
| if (num % 100 >= 11 && num % 100 <= 13) { | ||
| return num + "th"; | ||
| } |
| // Case 7: Identify the ordinal number for 102 | ||
| // When the number is 102, | ||
| // Then the function should return "102nd" | ||
|
|
||
| test("should return '102nd' for 102", () => { | ||
| expect(getOrdinalNumber(102)).toEqual("102nd"); | ||
| }); |
There was a problem hiding this comment.
To ensure thorough testing, we need broad scenario coverage. 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(102) ).toEqual("102nd");
});
There was a problem hiding this comment.
Thank you for the feedback I have grouped all the different teste-cases to categories.
some of the benefits of this approach are:
- Scalability — We don't need to test every number, just representatives of each category.
- Maintainability — It is easier to read and update tests when logic changes.
- Comprehensive — It covers all the functional branches in the logic.
- Avoids Redundancy — We won't waste time testing repetitive patterns unnecessarily.
Sprint-3/2-practice-tdd/repeat.js
Outdated
| function repeat() { | ||
| return "hellohellohello"; | ||
| if (arguments.length !== 2) { | ||
| throw new Error("Function requires exactly two arguments: str and count."); | ||
| } | ||
|
|
||
| const [str, count] = arguments; | ||
|
|
There was a problem hiding this comment.
Why not just declare the parameters as
function repeat(str, count) {
...?
There was a problem hiding this comment.
Yes we can declare the parameters in the function call and we will not need to declare const [str, count] = arguments;.
Sprint-3/2-practice-tdd/repeat.js
Outdated
| throw new Error("First argument must be a string."); | ||
| } | ||
|
|
||
| if (typeof count !== "number" || !Number.isInteger(count) || count < 0) { |
There was a problem hiding this comment.
Similar to the case in get-ordinal-number.js,
can you think of a more concise way to express the condition of this if statement?
There was a problem hiding this comment.
There are two other ways that this condition can be expressed concisely.
concise (but potentially less readable):
if (!(Number.isInteger(count) && count >= 0))
and the other concise but more readable option:
if (!Number.isSafeInteger(count) || count < 0)
|
In your PR description, the "Changelist" section is missing a header. |
|
|
||
| test("should return '4th' for 4", () => { | ||
| // category 4: all other numbers -> "th" | ||
| test("append 'th' to all other numbers", () => { |
There was a problem hiding this comment.
When this test fails, the description "all other numbers" is not quite informative; one would need to read the other tests to figure out what numbers are considered as "other numbers".
There was a problem hiding this comment.
fixed to more detailed description, "append 'th' to numbers ending in 0, 4–9, or 11–13"
| expect(getOrdinalNumber(12)).toEqual("12th"); | ||
| expect(getOrdinalNumber(13)).toEqual("13th"); | ||
| expect(getOrdinalNumber(14)).toEqual("14th"); | ||
| expect(getOrdinalNumber(111)).toEqual("111th"); | ||
| expect(getOrdinalNumber(112)).toEqual("112th"); | ||
| expect(getOrdinalNumber(113)).toEqual("113th"); |
There was a problem hiding this comment.
Why test the same numbers again in a separate test?
There was a problem hiding this comment.
You are right all the test under category 5: special case for 11, 12, 13 -> "th" can be removed since they are tested in category 4: all other numbers -> "th"


Self checklist
Changelist
fixed test-case functions and created varies test cases to make sure the testcase function works correctly and passes all test case scenarios.