-
-
Notifications
You must be signed in to change notification settings - Fork 218
London | 25-ITP-Sep | Adnaan Abo | Sprint 3 | coursework/sprint-3-practice-tdd #718
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?
London | 25-ITP-Sep | Adnaan Abo | Sprint 3 | coursework/sprint-3-practice-tdd #718
Conversation
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.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you think of a more concise way to express the condition of this if statement?
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.
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"; | ||
} |
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.
Indentation is a bit off.
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.
Indentation is now fixed
// 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.
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 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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why not just declare the parameters as
function repeat(str, count) {
...
?
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.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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. |
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.
Changes look good. I just have a few more suggestions.
Also, in the PR description, can you make the "Changelist" header to look like this?
Changelist
|
||
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why test the same numbers again in a separate test?
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.
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.