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