-
-
Notifications
You must be signed in to change notification settings - Fork 339
NW | 25-ITP_Sep | Ahmad Hmedan | Sprint 3 | implement and rewrite tests #812
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
Changes from 8 commits
3760f67
9da6cfa
12ac0d3
b8f4270
4a5a46b
331e4ff
2163ec2
ebad589
3e9ab28
922e296
cf43501
1bb6c7e
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -5,11 +5,17 @@ | |
| // Written here like this: 1/2 == Numerator/Denominator | ||
| // the first test and first case is written for you | ||
| // complete the rest of the tests and cases | ||
| // write one test at a time, and make it pass, build your solution up methodically | ||
| // write one test at a time, and make it pass, build your solution up | ||
|
|
||
| function isProperFraction(numerator, denominator) { | ||
| if (denominator === 0) return false; | ||
| if (numerator < 1) numerator = numerator * -1; | ||
| if (denominator < 1) denominator = Math.abs(denominator); | ||
|
Comment on lines
+12
to
13
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
|
|
||
| if (numerator < denominator) { | ||
| return true; | ||
| } else { | ||
| return false; | ||
| } | ||
| } | ||
|
|
||
|
|
@@ -47,13 +53,20 @@ assertEquals(improperFraction, false); | |
| // Explanation: The fraction -4/7 is a proper fraction because the absolute value of the numerator (4) is less than the denominator (7). The function should return true. | ||
| const negativeFraction = isProperFraction(-4, 7); | ||
| // ====> complete with your assertion | ||
| assertEquals(negativeFraction, true); | ||
|
|
||
| // Equal Numerator and Denominator check: | ||
| // Input: numerator = 3, denominator = 3 | ||
| // target output: false | ||
| // Explanation: The fraction 3/3 is not a proper fraction because the numerator is equal to the denominator. The function should return false. | ||
| const equalFraction = isProperFraction(3, 3); | ||
| // ====> complete with your assertion | ||
|
|
||
| assertEquals(equalFraction, false); | ||
| // Stretch: | ||
| // What other scenarios could you test for? | ||
| //Negative fraction check | ||
| //Input: numerator=-4, denominator=2; | ||
| //target output: false | ||
| // Explanation: The fraction -4/2 is not a proper fraction because the absolute value of the numerator (4) is more than the denominator (2). The function should return false. | ||
| const negativeFractionCase2 = isProperFraction(-4, 2); | ||
| assertEquals(negativeFractionCase2, false); | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -8,11 +8,20 @@ | |
| // write one test at a time, and make it pass, build your solution up methodically | ||
| // just make one change at a time -- don't rush -- programmers are deep and careful thinkers | ||
| function getCardValue(card) { | ||
| const rank = card.slice(0, -1); | ||
| if (rank === "A") { | ||
| return 11; | ||
| } | ||
| if (rank === "J" || rank === "Q" || rank === "K") { | ||
| return 10; | ||
| } | ||
| const convertTheStringtoNumber = Number(rank); | ||
| if (convertTheStringtoNumber >= 2 && convertTheStringtoNumber <= 10) { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 To find out what these strings are, you can ask AI
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thanks, I’ve learned a new validation technique from your feedback. |
||
| return convertTheStringtoNumber; | ||
| } else { | ||
| return "Invalid card rank"; | ||
| } | ||
| } | ||
|
|
||
| // The line below allows us to load the getCardValue function into tests in other files. | ||
| // This will be useful in the "rewrite tests with jest" step. | ||
| module.exports = getCardValue; | ||
|
|
@@ -40,18 +49,26 @@ assertEquals(aceofSpades, 11); | |
| // Then it should return the numeric value corresponding to the rank (e.g., "5" should return 5). | ||
| const fiveofHearts = getCardValue("5♥"); | ||
| // ====> write your test here, and then add a line to pass the test in the function above | ||
|
|
||
| assertEquals(fiveofHearts, 5); | ||
| // Handle Face Cards (J, Q, K): | ||
| // Given a card with a rank of "10," "J," "Q," or "K", | ||
| // When the function is called with such a card, | ||
| // Then it should return the value 10, as these cards are worth 10 points each in blackjack. | ||
|
|
||
| const jackofHearts = getCardValue("J♥"); | ||
| assertEquals(jackofHearts, 10); | ||
| const queenofSpades = getCardValue("Q♠"); | ||
| assertEquals(queenofSpades, 10); | ||
| const kingofSpades = getCardValue("K♠"); | ||
| assertEquals(kingofSpades, 10); | ||
| // Handle Ace (A): | ||
| // Given a card with a rank of "A", | ||
| // When the function is called with an Ace, | ||
| // Then it should, by default, assume the Ace is worth 11 points, which is a common rule in blackjack. | ||
|
|
||
| const aceofHearts = getCardValue("A♥"); | ||
| assertEquals(aceofHearts, 11); | ||
| // Handle Invalid Cards: | ||
| // Given a card with an invalid rank (neither a number nor a recognized face card), | ||
| // When the function is called with such a card, | ||
| // Then it should throw an error indicating "Invalid card rank." | ||
| const invalidCards = getCardValue("B♥"); | ||
| assertEquals(invalidCards, "Invalid card rank"); | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -7,7 +7,14 @@ test("should return true for a proper fraction", () => { | |
| }); | ||
|
|
||
| // Case 2: Identify Improper Fractions: | ||
|
|
||
| test("returns false for an improper fraction", () => { | ||
| expect(isProperFraction(5, 2)).toEqual(false); | ||
| }); | ||
| // Case 3: Identify Negative Fractions: | ||
|
|
||
| test("returns true for a negative proper fraction( absolute value of the numerator is less than the denominator)", () => { | ||
| expect(isProperFraction(-4, 7)).toEqual(true); | ||
| }); | ||
|
Comment on lines
+14
to
+16
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. When preparing tests, we should ensure the tests cover all possible cases (and maybe test multiple samples within each case to make the test more robust). Can you think of case(s) that should also be tested? |
||
| // Case 4: Identify Equal Numerator and Denominator: | ||
| test("returns false when numerator equals denominator", () => { | ||
| expect(isProperFraction(9, 9)).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.
Why use different approaches to remove the negative sign from a number?
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 don't know if you want me to use an existing library or solve it manually.
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 real interview, developers are expected to explain their code and give a rational explanation why they do things in certain way.
Your code looks odd to me, that's why I raised the question.
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 for your feedback. From now on, I’ll try to write my code as if I’m working.