|
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, -1); |
| 12 | + if (["2", "3", "4", "5", "6", "7", "8", "9"].includes(rank)) { |
| 13 | + return parseInt(rank, 10); |
| 14 | + } |
11 | 15 | if (rank === "A") { |
12 | 16 | return 11; |
13 | 17 | } |
| 18 | + if (["K", "Q", "J", "10"].includes(rank)) { |
| 19 | + return 10; |
| 20 | + } |
| 21 | + return "Invalid card rank"; |
14 | 22 | } |
15 | 23 |
|
16 | 24 | // The line below allows us to load the getCardValue function into tests in other files. |
@@ -39,19 +47,41 @@ assertEquals(aceofSpades, 11); |
39 | 47 | // When the function is called with such a card, |
40 | 48 | // Then it should return the numeric value corresponding to the rank (e.g., "5" should return 5). |
41 | 49 | const fiveofHearts = getCardValue("5♥"); |
| 50 | +assertEquals(fiveofHearts, 5); |
42 | 51 | // ====> write your test here, and then add a line to pass the test in the function above |
43 | 52 |
|
44 | 53 | // Handle Face Cards (J, Q, K): |
45 | 54 | // Given a card with a rank of "10," "J," "Q," or "K", |
46 | 55 | // When the function is called with such a card, |
47 | 56 | // Then it should return the value 10, as these cards are worth 10 points each in blackjack. |
48 | 57 |
|
| 58 | +const kingofDiamonds = getCardValue("K♦"); |
| 59 | +assertEquals(kingofDiamonds, 10); |
| 60 | +const queenofClubs = getCardValue("Q♣"); |
| 61 | +assertEquals(queenofClubs, 10); |
| 62 | +const jackofHearts = getCardValue("J♥"); |
| 63 | +assertEquals(jackofHearts, 10); |
| 64 | +const tenofSpades = getCardValue("10♠"); |
| 65 | +assertEquals(tenofSpades, 10); |
49 | 66 | // Handle Ace (A): |
50 | 67 | // Given a card with a rank of "A", |
51 | 68 | // When the function is called with an Ace, |
52 | 69 | // Then it should, by default, assume the Ace is worth 11 points, which is a common rule in blackjack. |
53 | 70 |
|
| 71 | +const aceofClubs = getCardValue("A♣"); |
| 72 | +assertEquals(aceofClubs, 11); |
| 73 | + |
54 | 74 | // Handle Invalid Cards: |
55 | 75 | // Given a card with an invalid rank (neither a number nor a recognized face card), |
56 | 76 | // When the function is called with such a card, |
57 | 77 | // Then it should throw an error indicating "Invalid card rank." |
| 78 | +const invalidCard = getCardValue("1♠"); |
| 79 | +assertEquals(invalidCard, "Invalid card rank"); |
| 80 | +const anotherInvalidCard = getCardValue("B♦"); |
| 81 | +assertEquals(anotherInvalidCard, "Invalid card rank"); |
| 82 | + |
| 83 | +console.log(getCardValue("3♠")); |
| 84 | +console.log(getCardValue("10♣")); |
| 85 | +console.log(getCardValue("J♦")); |
| 86 | +console.log(getCardValue("A♥")); |
| 87 | +console.log(getCardValue("11♠")); |
0 commit comments