|
8 | 8 | // write one test at a time, and make it pass, build your solution up methodically |
9 | 9 | // just make one change at a time -- don't rush -- programmers are deep and careful thinkers |
10 | 10 | function getCardValue(card) { |
| 11 | + |
| 12 | + const rank = card.slice(0, -1); // Extract rank before the suit |
| 13 | + |
11 | 14 | if (rank === "A") { |
12 | 15 | return 11; |
| 16 | + } else if (["K", "Q", "J", "10"].includes(rank)) { |
| 17 | + return 10; |
| 18 | + } else if (!isNaN(rank) && Number(rank) >= 2 && Number(rank) <= 9) { |
| 19 | + return Number(rank); |
| 20 | + } else { |
| 21 | + throw new Error("Invalid card rank"); |
13 | 22 | } |
| 23 | + |
| 24 | + |
14 | 25 | } |
15 | 26 |
|
16 | 27 | // The line below allows us to load the getCardValue function into tests in other files. |
@@ -40,18 +51,41 @@ assertEquals(aceofSpades, 11); |
40 | 51 | // Then it should return the numeric value corresponding to the rank (e.g., "5" should return 5). |
41 | 52 | const fiveofHearts = getCardValue("5♥"); |
42 | 53 | // ====> write your test here, and then add a line to pass the test in the function above |
| 54 | +assertEquals(fiveofHearts, 5); |
43 | 55 |
|
44 | 56 | // Handle Face Cards (J, Q, K): |
45 | 57 | // Given a card with a rank of "10," "J," "Q," or "K", |
46 | 58 | // When the function is called with such a card, |
47 | 59 | // Then it should return the value 10, as these cards are worth 10 points each in blackjack. |
| 60 | +const kingofDiamonds = getCardValue("K♦"); |
| 61 | +assertEquals(kingofDiamonds, 10); |
| 62 | +const jackofClubs = getCardValue("J♣"); |
| 63 | +assertEquals(jackofClubs, 10); |
| 64 | +const queenofHearts = getCardValue("Q♥"); |
| 65 | +assertEquals(queenofHearts, 10); |
| 66 | +const tenofSpades = getCardValue("10♠"); |
| 67 | +assertEquals(tenofSpades, 10); |
48 | 68 |
|
49 | 69 | // Handle Ace (A): |
50 | 70 | // Given a card with a rank of "A", |
51 | 71 | // When the function is called with an Ace, |
52 | 72 | // Then it should, by default, assume the Ace is worth 11 points, which is a common rule in blackjack. |
| 73 | +const aceofClubs = getCardValue("A♣"); |
| 74 | +assertEquals(aceofClubs, 11); |
53 | 75 |
|
54 | 76 | // Handle Invalid Cards: |
55 | 77 | // Given a card with an invalid rank (neither a number nor a recognized face card), |
56 | 78 | // When the function is called with such a card, |
57 | 79 | // Then it should throw an error indicating "Invalid card rank." |
| 80 | +const invalidCard = () => getCardValue("1♠"); |
| 81 | +// ====> write your test here, and then add a line to pass the test in the function above |
| 82 | +try { |
| 83 | + invalidCard(); |
| 84 | + console.assert(false, "Expected an error to be thrown for invalid card rank"); |
| 85 | +} catch (e) { |
| 86 | + console.assert( |
| 87 | + e.message === "Invalid card rank", |
| 88 | + `Expected error message to be "Invalid card rank" but got "${e.message}"` |
| 89 | + ); |
| 90 | +} |
| 91 | + |
0 commit comments