-
-
Notifications
You must be signed in to change notification settings - Fork 240
West Midlands | ITP-SEPT-25 | Georgina Rogers | Sprint 3 | Implement and rewrite tests #846
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 all commits
71394a8
0166d84
94d470e
ec1257e
2ef15ec
1e11b37
2547a1a
1c88d16
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 |
|---|---|---|
| @@ -1,16 +1,17 @@ | ||
| // This problem involves playing cards: https://en.wikipedia.org/wiki/Standard_52-card_deck | ||
|
|
||
| // You will need to implement a function getCardValue | ||
| // the function takes a single parameter, a string representing a playing card | ||
| // the function should return the numerical value of the card | ||
| // 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 | ||
| // 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 (!isNaN(rank) && Number(rank) >= 2 && Number(rank) <= 10) { | ||
| // checks for number cards 2-10 | ||
| return Number(rank); | ||
| } | ||
|
Comment on lines
+6
to
+9
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
or
|
||
| if (["J", "Q", "K"].includes(rank)) { | ||
| return 10; | ||
| } else { | ||
| throw new Error("Invalid card rank"); | ||
| } | ||
| } | ||
|
|
||
| // The line below allows us to load the getCardValue function into tests in other files. | ||
|
|
@@ -26,32 +27,49 @@ function assertEquals(actualOutput, targetOutput) { | |
| `Expected ${actualOutput} to equal ${targetOutput}` | ||
| ); | ||
| } | ||
| // Acceptance criteria: | ||
|
|
||
| // Case 1 | ||
| // Given a card string in the format "A♠" (representing a card in blackjack - the last character will always be an emoji for a suit, and all characters before will be a number 2-10, or one letter of J, Q, K, A), | ||
| // When the function getCardValue is called with this card string as input, | ||
| // Then it should return the numerical card value | ||
| const aceofSpades = getCardValue("A♠"); | ||
| assertEquals(aceofSpades, 11); | ||
|
|
||
| // Case 2 | ||
| // 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); | ||
|
|
||
| // Case 3 | ||
| // 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); | ||
|
|
||
| // Case 4 | ||
| // 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); | ||
|
|
||
| // Case 5 | ||
| // 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." | ||
| try { | ||
| getCardValue("1♣"); | ||
| console.assert(false, "Expected error for invalid card"); | ||
| } catch (error) { | ||
| console.assert( | ||
| error.message === "Invalid card rank", | ||
| `Expected "Invalid card rank" error, but got: ${error.message}` | ||
| ); | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -7,7 +7,16 @@ test("should return true for a proper fraction", () => { | |
| }); | ||
|
|
||
| // Case 2: Identify Improper Fractions: | ||
| test("should return false for an improper fraction", () => { | ||
| expect(isProperFraction(5, 4)).toEqual(false); | ||
| }); | ||
|
Comment on lines
+10
to
+12
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. We could test multiple values (including negative numbers) within this test to make the coverage more complete. |
||
|
|
||
| // Case 3: Identify Negative Fractions: | ||
| test("should return false for a negative fraction", () => { | ||
| expect(isProperFraction(-3, 4)).toEqual(true); | ||
| }); | ||
zilinskyte marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| // Case 4: Identify Equal Numerator and Denominator: | ||
| test("should return false for equal numerator and denominator", () => { | ||
| expect(isProperFraction(3, 3)).toEqual(false); | ||
| }); | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -8,6 +8,25 @@ test("should return 11 for Ace of Spades", () => { | |
| }); | ||
|
|
||
| // Case 2: Handle Number Cards (2-10): | ||
| test("should return 7 for 7 of Hearts", () => { | ||
| const sevenOfHearts = getCardValue("7♥"); | ||
| expect(sevenOfHearts).toEqual(7); | ||
| }); | ||
|
|
||
|
Comment on lines
+11
to
+15
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. 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 |
||
| // Case 3: Handle Face Cards (J, Q, K): | ||
| test("should return 10 for King of Hearts", () => { | ||
| const kingOfHearts = getCardValue("K♥"); | ||
| expect(kingOfHearts).toEqual(10); | ||
| }); | ||
|
|
||
|
Comment on lines
+17
to
+21
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 function is not expected to test the suit character. Describing the suit character in the test description could mislead the person implementing the function into thinking the function needs also to check the suit character. |
||
| // Case 4: Handle Ace (A): | ||
| test("should return 11 for Ace of Diamonds", () => { | ||
| const aceOfDiamonds = getCardValue("A♦"); | ||
| expect(aceOfDiamonds).toEqual(11); | ||
| }); | ||
|
|
||
| // Case 5: Handle Invalid Cards: | ||
| test("should return 0 for invalid card", () => { | ||
| const invalidCard = getCardValue("1♣"); | ||
| expect(invalidCard).toEqual(0); | ||
| }); | ||
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 make this function to work also for negative parameters? For example, make
isProperFraction(4, -5)evaluates totrue.