-
-
Notifications
You must be signed in to change notification settings - Fork 337
London | 26-ITP-Jan | Abdul Moiz | Sprint 3 | implement-and-rewrite #915
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?
Changes from 8 commits
5f845d0
2b5822f
a08e5c7
05ac5d6
b7ff208
2fdc1ca
d6ce110
1ba0eff
7d4b528
ac777a7
03452aa
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 |
|---|---|---|
|
|
@@ -22,7 +22,16 @@ | |
| // execute the code to ensure all tests pass. | ||
|
|
||
| function getCardValue(card) { | ||
| // TODO: Implement this function | ||
| const rank = card.slice(0, card.length - 1); | ||
|
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. The requirements has changed a bit. |
||
| if (rank === "A") { | ||
| return 11; | ||
| } else if (Number(rank) >= 2 && Number(rank) < 10) { | ||
| return Number(rank); | ||
|
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. I added an if statement beforehand to check typeof for the variable. If it fails it will throw an error. |
||
| } else if (rank === "10" || rank === "J" || rank === "Q" || rank === "K") { | ||
| return 10; | ||
| } else { | ||
| return "Invalid card rank"; | ||
| } | ||
|
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. The new requirement is:
Throwing an error is not the same as returning an error message.
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. Modified function and test case to throw error.
Comment on lines
+36
to
+45
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.
I don't see any statement involving Currently, a card value like ""0x02♠" or "3.1416♠" can still pass the check on line 37.
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. Apologies, I got it mixed up with a different task. It is resolved now. |
||
| } | ||
|
|
||
| // The line below allows us to load the getCardValue function into tests in other files. | ||
|
|
@@ -41,12 +50,31 @@ function assertEquals(actualOutput, targetOutput) { | |
| // Examples: | ||
| assertEquals(getCardValue("9♠"), 9); | ||
|
|
||
| // Handling invalid cards | ||
| try { | ||
| getCardValue("invalid"); | ||
| // Handle Number Cards (2-10): | ||
| // Given a card with a rank between "2" and "9", | ||
| // When the function is called with such a card, | ||
| // 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); | ||
|
|
||
| // This line will not be reached if an error is thrown as expected | ||
| console.error("Error was not thrown for invalid card"); | ||
| } catch (e) {} | ||
| // 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 kingofDiamonds = getCardValue("K♦"); | ||
| assertEquals(kingofDiamonds, 10); | ||
|
|
||
| // What other invalid card cases can you think of? | ||
| // 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 aceofClubs = getCardValue("A♣"); | ||
| assertEquals(aceofClubs, 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 invalidCard = getCardValue("1♠"); | ||
| assertEquals(invalidCard, "Invalid card rank"); | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -8,3 +8,19 @@ const isProperFraction = require("../implement/2-is-proper-fraction"); | |
| test(`should return false when denominator is zero`, () => { | ||
| expect(isProperFraction(1, 0)).toEqual(false); | ||
| }); | ||
|
|
||
| test(`should return true for proper fraction with positive numerator and denominator`, () => { | ||
| expect(isProperFraction(1, 2)).toEqual(true); | ||
| }); | ||
|
|
||
| test(`should return false for improper fraction with positive numerator and denominator`, () => { | ||
| expect(isProperFraction(3, 2)).toEqual(false); | ||
| }); | ||
|
|
||
|
Comment on lines
+12
to
+19
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. What about negative numbers and different combinations of positive/negative/zero? Think also what edge case to check?
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. Added extra tests for negative numerators/denominators and all are passing. |
||
| test(`should return true for numerator zero and positive denominator`, () => { | ||
| expect(isProperFraction(0, 5)).toEqual(true); | ||
| }); | ||
|
|
||
| test(`should return false for denominator zero`, () => { | ||
| expect(isProperFraction(5, 0)).toEqual(false); | ||
| }); | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -14,7 +14,27 @@ test(`Should return 11 when given an ace card`, () => { | |
| // Face Cards (J, Q, K) | ||
| // Invalid Cards | ||
|
|
||
| // Case 2: Number Cards (2-10) | ||
| test(`Should return the correct value for number cards`, () => { | ||
| expect(getCardValue("2♣")).toEqual(2); | ||
| expect(getCardValue("5♦")).toEqual(5); | ||
| expect(getCardValue("10♥")).toEqual(10); | ||
| }); | ||
|
|
||
| // Case 3: Face Cards (J, Q, K) | ||
| test(`Should return 10 for face cards`, () => { | ||
| expect(getCardValue("J♠")).toEqual(10); | ||
| expect(getCardValue("Q♦")).toEqual(10); | ||
| expect(getCardValue("K♥")).toEqual(10); | ||
| }); | ||
|
|
||
| // Case 4: Invalid Cards | ||
| test(`Should return Invalid card rank for invalid cards`, () => { | ||
| expect(getCardValue("1♠")).toEqual("Invalid card rank"); | ||
| expect(getCardValue("11♥")).toEqual("Invalid card rank"); | ||
| expect(getCardValue("Z♦")).toEqual("Invalid card rank"); | ||
| }); | ||
|
|
||
|
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.
|
||
| // To learn how to test whether a function throws an error as expected in Jest, | ||
| // please refer to the Jest documentation: | ||
| // https://jestjs.io/docs/expect#tothrowerror | ||
|
|
||
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 works.
Optional challenge: Simplify the code.