|
1 | 1 | // This problem involves playing cards: https://en.wikipedia.org/wiki/Standard_52-card_deck |
2 | 2 |
|
3 | | -// You will need to implement a function getCardValue |
4 | | -// the function takes a single parameter, a string representing a playing card |
5 | | -// the function should return the numerical value of the card |
6 | | -// the first test and first case is written for you |
7 | | -// complete the rest of the tests and cases |
8 | | -// write one test at a time, and make it pass, build your solution up methodically |
9 | | -// just make one change at a time -- don't rush -- programmers are deep and careful thinkers |
| 3 | +// Implement a function getCardValue, when given a string representing a playing card, |
| 4 | +// should return the numerical value of the card. |
| 5 | + |
| 6 | +// A valid card string will contain a rank followed by the suit. |
| 7 | +// The rank can be one of the following strings: |
| 8 | +// "A", "2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K" |
| 9 | +// The suit can be one of the following emojis: |
| 10 | +// "♠", "♥", "♦", "♣" |
| 11 | +// For example: "A♠", "2♥", "10♥", "J♣", "Q♦", "K♦". |
| 12 | + |
| 13 | +// When the card is an ace ("A"), the function should return 11. |
| 14 | +// When the card is a face card ("J", "Q", "K"), the function should return 10. |
| 15 | +// When the card is a number card ("2" to "10"), the function should return its numeric value. |
| 16 | + |
| 17 | +// When the card string is invalid (not following the above format), the function should |
| 18 | +// throw an error. |
| 19 | + |
| 20 | +// Acceptance criteria: |
| 21 | +// After you have implemented the function, write tests to cover all the cases, and |
| 22 | +// execute the code to ensure all tests pass. |
| 23 | + |
10 | 24 | function getCardValue(card) { |
11 | | - if (rank === "A") { |
12 | | - return 11; |
13 | | - } |
| 25 | + // TODO: Implement this function |
14 | 26 | } |
15 | 27 |
|
16 | 28 | // The line below allows us to load the getCardValue function into tests in other files. |
17 | 29 | // This will be useful in the "rewrite tests with jest" step. |
18 | 30 | module.exports = getCardValue; |
19 | 31 |
|
20 | | -// You need to write assertions for your function to check it works in different cases |
21 | | -// we're going to use this helper function to make our assertions easier to read |
22 | | -// if the actual output matches the target output, the test will pass |
| 32 | +// Helper functions to make our assertions easier to read. |
23 | 33 | function assertEquals(actualOutput, targetOutput) { |
24 | 34 | console.assert( |
25 | 35 | actualOutput === targetOutput, |
26 | 36 | `Expected ${actualOutput} to equal ${targetOutput}` |
27 | 37 | ); |
28 | 38 | } |
29 | | -// Acceptance criteria: |
30 | 39 |
|
31 | | -// 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), |
32 | | -// When the function getCardValue is called with this card string as input, |
33 | | -// Then it should return the numerical card value |
34 | | -const aceofSpades = getCardValue("A♠"); |
35 | | -assertEquals(aceofSpades, 11); |
36 | | - |
37 | | -// Handle Number Cards (2-10): |
38 | | -// Given a card with a rank between "2" and "9", |
39 | | -// When the function is called with such a card, |
40 | | -// Then it should return the numeric value corresponding to the rank (e.g., "5" should return 5). |
41 | | -const fiveofHearts = getCardValue("5♥"); |
42 | | -// ====> write your test here, and then add a line to pass the test in the function above |
43 | | - |
44 | | -// Handle Face Cards (J, Q, K): |
45 | | -// Given a card with a rank of "10," "J," "Q," or "K", |
46 | | -// When the function is called with such a card, |
47 | | -// Then it should return the value 10, as these cards are worth 10 points each in blackjack. |
48 | | - |
49 | | -// Handle Ace (A): |
50 | | -// Given a card with a rank of "A", |
51 | | -// When the function is called with an Ace, |
52 | | -// Then it should, by default, assume the Ace is worth 11 points, which is a common rule in blackjack. |
53 | | - |
54 | | -// Handle Invalid Cards: |
55 | | -// Given a card with an invalid rank (neither a number nor a recognized face card), |
56 | | -// When the function is called with such a card, |
57 | | -// Then it should throw an error indicating "Invalid card rank." |
| 40 | +// TODO: Write tests to cover all outcomes, including throwing errors for invalid cards. |
| 41 | +// Examples: |
| 42 | +assertEquals(getCardValue("9♠"), 9); |
| 43 | + |
| 44 | +// Handling invalid cards |
| 45 | +try { |
| 46 | + getCardValue("invalid"); |
| 47 | + |
| 48 | + // This line will not be reached if an error is thrown as expected |
| 49 | + console.error("Error was not thrown for invalid card"); |
| 50 | +} catch (e) {} |
| 51 | + |
| 52 | +// What other invalid card cases can you think of? |
0 commit comments