-
-
Notifications
You must be signed in to change notification settings - Fork 262
Glasgow | 25-ITP-SEP | Mohammed Abdoon | Sprint 3 | Coursework/1-implement-and-rewrite-tests #745
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?
Glasgow | 25-ITP-SEP | Mohammed Abdoon | Sprint 3 | Coursework/1-implement-and-rewrite-tests #745
Conversation
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.
-
The files in
2-practice-tddsubfolder are not related to this exercise. Can you revert the changes made in that folder to keep this branch clean? -
Can you rename this branch to the name specified in the backlog?
| if(typeof(numerator) != "number" || typeof(denominator) != "number") { | ||
| return "[ wrong input ]"; | ||
| } | ||
| if (numerator < denominator ) { | ||
| return true; | ||
| } | ||
| 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.
In mathematics, -A/B == A/-B == -(A/B), and -A/-B == A/B for any integers A and B (B ≠ 0).
They represent a proper fraction if A < B and B ≠ 0.
So isProperFraction(-4, 3) should return false because 4 >= 3.
So isProperFraction(-2, 5) should return true because 2 < 5.
Consider comparing the absolute value of the numerator and the denominator instead.
In addition, to indicate errors, it is best practices to throw an error (instead of returning an error message).
If you have time, you should check out how to throw an error in JS.
Alternatively, you can choose to return false to indicate something is not a fraction.
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 made the required changes, I used the method Math.abs() to fix the bug and used error throwing when inputs are not valid. also the function should return 0 when denominator is 0;
Sprint-3/1-implement-and-rewrite-tests/implement/2-is-proper-fraction.js
Outdated
Show resolved
Hide resolved
| if ((rank => 2) && (rank <= 10)) { | ||
| return Number(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.
In JavaScript, strings that represent valid numeric literals in the language can be safely converted to equivalent numbers. For examples, "0x02", "2.1", or "0002".
Does your function return the value you expected from each of the following function calls?
getCardValue("0x02♠");
getCardValue("2.1♠");
getCardValue("0002♠");
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 pointing that out. Yes, the function returns what is expected, but I used Math.floor() to exclude decimal numbers.
| test("should identify Reflex angle (greater than 180° and less than 360°)", () => { | ||
| expect(getAngleType(181)).toEqual("Reflex angle"); | ||
| }); |
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.
We could specify multiple expect(...) statements within each test() to cover multiple values that belong to the same case. For example,
test("should identify reflex angle (180 < angle < 360)", () => {
expect(getAngleType(300)).toEqual("Reflex angle");
expect(getAngleType(359.999)).toEqual("Reflex angle");
expect(getAngleType(180.001)).toEqual("Reflex angle");
});
| test("should return true for a negative Fractions", () => { | ||
| expect(isProperFraction(-6, 4)).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.
-6/4 is not a proper fraction.
You can test several "negative fractions in one test". For examples,
test("should return true for a negative Fractions", () => {
expect(isProperFraction(-6, 4)).toEqual(false);
expect(isProperFraction(-4, 6)).toEqual(true);
expect(isProperFraction(6, -4)).toEqual(false);
expect(isProperFraction(4, -6)).toEqual(true);
expect(isProperFraction(-6, -4)).toEqual(false);
expect(isProperFraction(-4, -6)).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.
Done. I added more test cases to each category.
| test("should return the number entered as input", () => { | ||
| const numberCards = getCardValue("4♠"); | ||
| expect(numberCards).toEqual(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.
When preparing tests, we should ensure the tests cover all possible cases. If we specify a test for individual card, we will need about 53 tests to cover all possible cases. Instead, we could consider classifying all possible values into different categories, and then within each category we test some samples.
For example, one possible category for getCardValue() is, "should return the value of number cards (2-10)", and we can prepare the test as
test("should return the value of number cards (2-10)", () => {
expect(getCardValue("2♣︎")).toEqual(2);
expect(getCardValue("5♠")).toEqual(5);
expect(getCardValue("10♥")).toEqual(10);
// Note: We could also use a loop to check all values from 2 to 10.
});
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.
Ok. I changed the code and added more test cases + added different statements using for loop to test numbers from 2 to 10
Ok, I will delete the unnecessery folder, thank you. |
|
We can rename the remote branch directly on GitHub and keep the PR stay opened. Go ahead and backup your files and try. This is a learning experience and there won't be any penalty. |
It didn’t work. I tried renaming it directly on GitHub and also tried renaming it locally and pushing it, but the PR still gets closed. I even asked different AI tools, ChatGPT, Copilot, and Gemini, and they all ended up saying that if I want to keep my PR, I just don’t rename it! I guess I’ll need extra help; maybe I’ll ask a volunteer to help me during the workshop next Saturday. |
|
You are right. The PR will be closed when the branch is renamed. I remembered incorrectly. Sorry about that. You can just keep this branch and PR (instead of opening a new one). |
…erent case tests for the getCardValue function - looped inputs too
|
No worries at all, I learned several things while trying to rename the branch without having it closed :) The required changes have been made, thank you. |
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.
Changes look good. I have few more suggestions but I will go ahead and mark this PR as complete first as it has met all the basic requirements.
Sprint-3/1-implement-and-rewrite-tests/implement/2-is-proper-fraction.js
Outdated
Show resolved
Hide resolved
Sprint-3/1-implement-and-rewrite-tests/implement/2-is-proper-fraction.js
Outdated
Show resolved
Hide resolved
Sprint-3/1-implement-and-rewrite-tests/implement/2-is-proper-fraction.js
Outdated
Show resolved
Hide resolved
| rank = Number(Math.floor(rank)); | ||
|
|
||
| if ((rank >= 2) && (rank <= 10)) { | ||
| return 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 approach ensures values like "3.1416" or "0x00002" get returned as an equivalent integer, but do you want to consider "3.1416♥" or "0x00002♠" valid card values?
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 want to exclude them, so I added this condition :
if(!Number.isInteger(Number(rank) ) || /^0x/i.test(rank)) {
return "Invalid card rank.";
}
to ensure that the number is an integer without a decimal part, and to make sure it isn’t a hexadecimal value by using a regex with 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.
If you are using this approach (to consider what to reject), you would need to consider all valid numeric literal representations in the JS language. You can look up how numbers can be represented in the JS language.
Instead of focusing on what values are invalid, we could focus on what values are valid.
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.
Ok, I understand.
Thank you so much.
Sprint-3/1-implement-and-rewrite-tests/rewrite-tests-with-jest/3-get-card-value.test.js
Show resolved
Hide resolved
Thank you so much. I applied your suggestions to my code and gained better knowledge from them. Thanks again :) |
Learners, PR Template
Self checklist
Changelist
I implemented and rewrote tests, figured the errors and tested the code. modified the files to meet the test requirements.
Questions
No questions for now, thank you.