|
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 | | - if (rank === "A") { |
| 11 | + const rank = card[0]; |
| 12 | + |
| 13 | + if (card === "A♠") { |
| 14 | + return 11; |
| 15 | + } else if (Number(rank) > 1 && Number(rank) < 10) { |
| 16 | + return Number(rank); |
| 17 | + } else if (rank === "10" || rank === "J" || rank === "Q" || rank === "K") { |
| 18 | + return 10; |
| 19 | + } else if (rank === "A") { |
12 | 20 | return 11; |
| 21 | + } else { |
| 22 | + return "Invalid card rank."; |
13 | 23 | } |
14 | 24 | } |
15 | 25 |
|
@@ -38,20 +48,25 @@ assertEquals(aceofSpades, 11); |
38 | 48 | // Given a card with a rank between "2" and "9", |
39 | 49 | // When the function is called with such a card, |
40 | 50 | // Then it should return the numeric value corresponding to the rank (e.g., "5" should return 5). |
41 | | -const fiveofHearts = getCardValue("5♥"); |
| 51 | +const fiveofHearts = getCardValue("9♥"); |
42 | 52 | // ====> write your test here, and then add a line to pass the test in the function above |
43 | | - |
| 53 | +assertEquals(fiveofHearts, 9); |
44 | 54 | // Handle Face Cards (J, Q, K): |
45 | 55 | // Given a card with a rank of "10," "J," "Q," or "K", |
46 | 56 | // When the function is called with such a card, |
47 | 57 | // Then it should return the value 10, as these cards are worth 10 points each in blackjack. |
48 | | - |
| 58 | +const faceCards = getCardValue("J♥"); |
| 59 | +assertEquals(faceCards, 10); |
49 | 60 | // Handle Ace (A): |
50 | 61 | // Given a card with a rank of "A", |
51 | 62 | // When the function is called with an Ace, |
52 | 63 | // Then it should, by default, assume the Ace is worth 11 points, which is a common rule in blackjack. |
| 64 | +const ace = getCardValue("A♥"); |
| 65 | +assertEquals(ace, 11); |
53 | 66 |
|
54 | 67 | // Handle Invalid Cards: |
55 | 68 | // Given a card with an invalid rank (neither a number nor a recognized face card), |
56 | 69 | // When the function is called with such a card, |
57 | 70 | // Then it should throw an error indicating "Invalid card rank." |
| 71 | +const invalidCard = getCardValue("15♠"); |
| 72 | +assertEquals(invalidCard, "Invalid card rank."); |
0 commit comments