-
-
Notifications
You must be signed in to change notification settings - Fork 262
Birmingham | 25-ITP-Sep | Ahmad Ehsas | Sprint 3 | Implement-and-rewrite-tests #735
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?
Conversation
…, and invalid cards
…he goal of this function is to count how many times a specific character appears in a given string
…lues and update the test case.
|
Your PR description contained template fields which weren't filled in. Check you've ticked everything in the self checklist, and that any sections which prompt you to fill in an answer are either filled in or removed. If this PR is not coursework, please add the NotCoursework label (and message on Slack in #cyf-curriculum or it will probably not be noticed). |
|
Thank you for your reviewing. I just have a question, should I make another
branch for ‘practice-tdd’ backlog or do that in the same branch?
…On Thu, 30 Oct 2025 at 15:58, CJ Yuan ***@***.***> wrote:
*cjyuan* left a comment
(CodeYourFuture/Module-Structuring-and-Testing-Data#735)
<#735 (comment)>
Good job in keeping the branch updated. Now let's focus on keeping the
branch clean.
On the upstream branch (CYF's main), the subfolders inside "Sprint-3"
folder have been renamed. As a result, the filenames are all "messed up",
and in short, that makes code reviewing difficult.
Can you transfer all of your changes to the files in these three
subfolders, and then remove all other subfolders in "Sprint-3" folder?
- Sprint-3/1-implement-and-rewrite-tests
- Sprint-3/2-practice-tdd
- Sprint-3/3-stretch
—
Reply to this email directly, view it on GitHub
<#735 (comment)>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/BOYJJVD77WICLBREAB6UM2L32IYRHAVCNFSM6AAAAACILF2MFGVHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMZTINRYG42DMMJQGI>
.
You are receiving this because you were mentioned.Message ID:
<CodeYourFuture/Module-Structuring-and-Testing-Data/pull/735/c3468746102@
github.com>
|
Either way works. |
cjyuan
left a comment
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.
- Good job in fixing the branch.
Sprint-3/1-implement-and-rewrite-tests/implement/1-get-angle-type.js
Outdated
Show resolved
Hide resolved
| if (Math.abs(numerator) < denominator) return true; // This version of code works correctly for proper and negative fractions. | ||
| if (Math.abs(numerator) >= Math.abs(denominator)) return false; | ||
| if (Math.abs(numerator) === Math.abs(denominator)) return false; | ||
|
|
||
| if (numerator < denominator) { | ||
| return true; | ||
| } |
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.
-
What do you expect from
isProperFraction(-2, -3)? -
Some of the code is redundant.
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.
- My expectation for 'isProperFraction (-2, -3) ': the fraction is proper because the absolute value of -2 / -3 is 2 / 3 and the numerator is less than the denominator, so the return should be true.
- The redundant code has been removed.
| if (!isNaN(rank)) { | ||
| return Number(rank); // Number card | ||
| } |
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.
In JavaScript, strings that represent valid numeric literals in the language can be safely converted to equivalent numbers. For examples, "0x02", "2.1", "9e1" or "0002"
Do you want to recognize these string values as valid ranks?
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 recognise string values as valid ranks, I used 'Number(rank)'
console.log(Number("0x002"));
console.log(Number("2.1"));
console.log(Number("9e1"));
console.log(Number("0002"));
| test("should return true for negative fractions", () => { | ||
| const negativeFraction = isProperFraction(-4, 7); | ||
| expect(negativeFraction).toEqual(true); | ||
| }); | ||
|
|
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.
Not all negative fractions are proper fractions. For example, -5/2.
If you allow negative denominator, then you could also test isProperFraction(4, -7), isProperFraction(-4, -7), isProperFraction(-7, 4), isProperFraction(-7, -4), isProperFraction(7, -4).
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.
I employed 'Math.abs(denominator)' to handle fractions with negative denominators.
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.
The function is correct. I was referring to test and the test description.
The test description, "should return true for negative fractions", is misleading because negative fractions can be proper or improper.
| test("throws an error or invalid card", () => { | ||
| expect(() => getCardValue("Z♠")).toThrow("Invalid card"); | ||
| }); | ||
|
|
||
| test("should throw error for malformed numeric input", () => { | ||
| expect(() => getCardValue("2.9999♠")).toThrow("Invalid card rank"); | ||
| }); | ||
|
|
||
| test("should throw error for repeated characters in rank", () => { | ||
| expect(() => getCardValue("3AAAA♠")).toThrow("Invalid card rank"); | ||
| }); | ||
|
|
||
| test("should throw error for number beyond valid range", () => { | ||
| expect(() => getCardValue("11♠")).toThrow("Invalid card rank"); | ||
| }); | ||
|
|
||
| test("should throw error for missing suit", () => { | ||
| expect(() => getCardValue("Q")).toThrow("Invalid card rank"); | ||
| }); | ||
|
|
||
| test("should throw error for non-string input", () => { | ||
| expect(() => getCardValue(5)).toThrow("Card must be a string"); | ||
| }); | ||
|
|
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 current implementation pass all these tests?
-
Why specifically test "repeated characters" in rank? What's so special about this particular string pattern?
-
"2.9999" is not really a malformed numeric value.
-
The function is not expected to check the suit character. Including a test for missing suit might send the wrong message to the person implementing the function.
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 specifically test "repeated characters" in rank? What's so special about this particular string pattern?
test("should throw error for repeated characters in rank", () => {
expect(() => getCardValue("3AAAA♠")).toThrow("Invalid card rank");
});
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 (Math.abs(numerator) < Math.abs(denominator)) return true; // This version of code works correctly for proper and negative fractions. | ||
| if (Math.abs(numerator) >= Math.abs(denominator)) return false; | ||
| if (Math.abs(numerator) === Math.abs(denominator)) return false; |
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.
-
Some of these code will never get executed.
-
If the condition of the if statements are opposite of each other, we could use
if-else.
| if (!/^[0-9]+$/.test(rank)) { | ||
| throw new Error("Invalid card rank"); | ||
| } |
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.
This is a stricter check. Still, any string containing only digits could pass this test. For example, "0000002".
| test("should return true for negative fractions", () => { | ||
| const negativeFraction = isProperFraction(-4, 7); | ||
| expect(negativeFraction).toEqual(true); | ||
| }); | ||
|
|
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.
The function is correct. I was referring to test and the test description.
The test description, "should return true for negative fractions", is misleading because negative fractions can be proper or improper.
| test("throws an error or invalid card", () => { | ||
| expect(() => getCardValue("Z♠")).toThrow("Invalid card"); | ||
| }); | ||
|
|
||
| test("should throw error for malformed numeric input", () => { | ||
| expect(() => getCardValue("2.9999♠")).toThrow("Invalid card rank"); | ||
| }); | ||
|
|
||
| test("should throw error for repeated characters in rank", () => { | ||
| expect(() => getCardValue("3AAAA♠")).toThrow("Invalid card rank"); | ||
| }); | ||
|
|
||
| test("should throw error for number beyond valid range", () => { | ||
| expect(() => getCardValue("11♠")).toThrow("Invalid card rank"); | ||
| }); | ||
|
|
||
| test("should throw error for missing suit", () => { | ||
| expect(() => getCardValue("Q")).toThrow("Invalid card rank"); | ||
| }); | ||
|
|
||
| test("should throw error for non-string input", () => { | ||
| expect(() => getCardValue(5)).toThrow("Card must be a string"); | ||
| }); | ||
|
|
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 specifically test "repeated characters" in rank? What's so special about this particular string pattern?
test("should throw error for repeated characters in rank", () => {
expect(() => getCardValue("3AAAA♠")).toThrow("Invalid card rank");
});
|
|
||
|
|
||
| // Anything else is invalid | ||
| throw new Error("Invalid card rank") |
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 figure out why this statement will never get executed?
| // Case 3: Identify Negative Fractions: | ||
| test("should return true when the absolute value of numerator is less than denominator, even if the fraction is negative", () => { | ||
| const negativeFraction = isProperFraction(-4, 7); | ||
| expect(negativeFraction).toEqual(true); | ||
| }); | ||
|
|
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.
Instead of just testing one particular form of negative fraction, we could also test (under the same category)
isProperFraction(4, -7);
isProperFraction(-4, -7);
isProperFraction(7, -4);
...
to make the test more complete.
| try { | ||
| getCardValue("Z♠"); | ||
| } catch (error) { | ||
| console.log("Caught error:", error.message); | ||
| } | ||
|
|
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.
Debugging code?

Self checklist
Changelist:
I have completed all the mandatory tasks. The changes I have made in the files are as below:
Questions