-
-
Notifications
You must be signed in to change notification settings - Fork 218
ITP-Jan 2025 | London | Maryna Romanova | Module-Structuring-and-Testing-Data | Week 6 | Sprint 3 #340
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
ITP-Jan 2025 | London | Maryna Romanova | Module-Structuring-and-Testing-Data | Week 6 | Sprint 3 #340
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.
Please check my Sprint 3
Can you fix this branch so that it only contains modified files in the "Sprint-3" folder? |
@cjyuan Done, the "Sprint-3" folder is fixed. Please review it. Many thanks. |
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 the functions could use some improvement or bug fix.
Most of my comments are related to Jest tests. Do you need assistance on using Jest?
function isProperFraction(numerator, denominator) { | ||
if (numerator < denominator) return true; | ||
else 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.
Mathematically speaking, -3/2 and -1/0 are not proper fractions (but -3 < 2 and -1 < 0) and -2/-3 is a proper fraction (but -2 > -3).
We need to consider the cases that numerator
and denominator
can be negative, positive, or zero.
However, if we were to consider all possible combinations of negative/positive/zero numerator and denominator, we would have to use a lot of if-statements. One way to reduce the complexity of the code is to "remove the negative sign" in numerator
and denominator
. If both numerator and denominator are non-negative numbers, then the if-statements at lines 11-12 would work perfectly.
Input normalisation, the process of pre-processing the input values before they are used in computation, can often simplify complexity of code, improve performance, and prevent errors.
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
if (rank === "A") return 11; | ||
} | ||
if (rank === "J" || rank === "Q" || rank === "K" || rank === "10") return 10; | ||
if (rank >= "2" && rank <= "9") return parseInt(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.
Does your function return the value you expected from each of the following function calls?
getCardValue("29♠");
getCardValue("0x02♠");
getCardValue("2.1♠")
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.
No, the function does not return this numeric. In all three cases, the function throws an "Invalid card rank" error.
test("should identify right angle (90°)", () => { | ||
expect(getAngleType(90)).toEqual("Right 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.
Why are all the tests testing the same thing (testing for "Right angle" 4 times) ?
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.
Sorry my mistake. Fixed
|
||
function assertEquals(actualOutput, targetOutput) { | ||
console.assert( | ||
actualOutput === targetOutput, | ||
`Expected ${actualOutput} to equal ${targetOutput}` | ||
); | ||
} |
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 to be tested in 2-is-proper-fraction.test.js
. So we don't need the assertEquals()
at lines 6-12.
After you reimplemented isProperFraction()
in 1-key-implement/2-is-proper-fractions.js
, you can copy the code of that function here.
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 deleted the lines 6-12, and fix the test in the file ' 2-is-proper-fraction.test.js '
const improperFraction = isProperFraction(5, 2); | ||
assertEquals(improperFraction, 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.
This exercise expects us to write Jest test code to test the function.
So to test improper fraction, we can express our test code as
test("should return false for a improper fraction", () => {
// We can test multiple sets of values belonging to the same case
expect(isProperFraction(5, 2)).toEqual(false);
expect(isProperFraction(5, -2)).toEqual(false);
expect(isProperFraction(-5, 2)).toEqual(false);
expect(isProperFraction(-5, -2)).toEqual(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.
got it
const repeatedStr = repeat(str, count); | ||
expect(repeatedStr).toEqual("hihihi"); | ||
}); | ||
|
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 about those cases described in the comments at lines 26-39?
For the negative count case, you can prepare your test as:
test("should throw an error when count is negative", () => {
expect(() => {
repeat('hi', -1)
}).toThrow();
});
Please note that when we expect a function to throw an error, we need to wrap the function call inside () => { ... }
.
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.
Thanks, very useful.
// a) How the index variable updates during the call to find - index start with O and going on with +1 until not reach str.length | ||
// b) What is the if statement used to check - in this situation "if" check if index finally the same with char, if it's the same the function returning index | ||
// c) Why is index++ being used? - it's increases index after each one while so we can't get stuck. | ||
// d) What is the condition index < str.length used for? - function doesn't work if the index higher then str.lenght. So function can understands in which limits need to work. |
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.
For question (d), suppose str
is "ABCD"
and char
is 'a'
.
- What is the value of
index
in the last iteration of the while-loop? - What is the value of
index
when the execution reaches line 10?
Why not try using an AI tool such as ChatGPT to check grammar in your writings?
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, I know that chat GPT is good for grammar correction, but I use it every day for my work, and I don't have enough practice to use my own brain, and I can't pass my Duo Lingo test (Just 80%), it's mean that I would be able to move to the another CYF's level. That why I'm trying to use my brain in the lessons to train my self.
Thanks for understanding and sorry for mistakes.
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 wouldn't encourage you to blindly copy the answer prepared by ChatGPT.
When I used ChatGPT to check grammar in my writing or improve my writing, it always explained why the corrected version was better. I find the explanation helpful in improving my writing skills.
function passwordValidator(password) { | ||
return password.length < 5 ? false : true | ||
function passwordValidator(password, passwords) { | ||
password.length < 5; |
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 statement does not do anything except comparing two values.
As a result, your function would not be able to return false
when the given password is "Aa1$".
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
You must breakdown this problem in order to solve it. Find one test case first and get that working | ||
*/ | ||
const isValidPassword = require("./password-validator"); | ||
|
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 Jest test code is missing in this file.
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.
Added
function cardValidator (card_number) { | ||
if (card_number.length === 16) { | ||
return true; | ||
} else { | ||
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 the card validation exercise, there are other requirements for a card number to be considered valid.
As this is the "stretch" part of the exercise, you can complete the implementation of this function when you have time.
@cjyuan Dear Cjyuan, I fixed all mistakes, please review it. |
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 think getCardValue()
can still use some improvement.
// // Case 5: Handle Invalid Cards: | ||
test("Handle Invalid Cards", () => { | ||
expect(() => getCardValue("Z♥")).toThrow("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.
Can you include these test values in this test, and then run this script using Jest? I don't think your script can recognise them as invalid cases (because of the way strings are compared).
getCardValue("29♠");
getCardValue("2.1♠");
If you have installed Jest, you can run this particular test script as npx jest 3-get-card-value.test.js
in the folder where this file is located.
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.
All tests pass !
test("should return '3rd' for 3", () => { | ||
|
||
|
||
test("should return '3rd' for 3 and more", () => { |
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 description of this test is not yet updated accordingly.
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.
@cjyuan All updated. Please approve it. |
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.
getCardValue()
is solid now.
One of the test descriptions could be improved. You can mark this as "Complete" after you update the description.
|
||
|
||
test("should return '3rd' for 3 and more", () => { | ||
test("should return all mentioned test bellow", () => { |
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 description does not tell what is being tested in this particular test.
When there is an error, Jest will display this message but not all of the code at lines 14-16. The person running the test would have to take an extra step to view the code in the test script to find out what the tests are about.
Learners, PR Template
Self checklist
Changelist
Briefly explain your PR.
Questions
Ask any questions you have for your reviewer.