|
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.slice(0, -1); |
| 12 | + const cardFace = card.slice(-1); |
| 13 | + const numRank = Number(rank); |
| 14 | + |
| 15 | + if ( |
| 16 | + rank === "A" && |
| 17 | + (cardFace === "♠" || |
| 18 | + cardFace === "♣" || |
| 19 | + cardFace === "♦" || |
| 20 | + cardFace === "♥") |
| 21 | + ) { |
12 | 22 | return 11; |
| 23 | + } else { |
| 24 | + ("Invalid card rank."); |
| 25 | + } |
| 26 | + |
| 27 | + if ( |
| 28 | + (rank === "10" || rank === "J" || rank === "Q" || rank === "K") && |
| 29 | + (cardFace === "♠" || |
| 30 | + cardFace === "♣" || |
| 31 | + cardFace === "♦" || |
| 32 | + cardFace === "♥") |
| 33 | + ) { |
| 34 | + return 10; |
| 35 | + } else { |
| 36 | + return "Invalid card rank."; |
| 37 | + } |
| 38 | + |
| 39 | + if ( |
| 40 | + Number.isInteger(numRank) && |
| 41 | + numRank >= 2 && |
| 42 | + numRank <= 9 && |
| 43 | + (cardFace === "♠" || |
| 44 | + cardFace === "♣" || |
| 45 | + cardFace === "♦" || |
| 46 | + cardFace === "♥") |
| 47 | + ) { |
| 48 | + console.log("numRank is", rank); |
| 49 | + return numRank; |
| 50 | + } else { |
| 51 | + return "Invalid card rank."; |
13 | 52 | } |
14 | 53 | } |
15 | 54 |
|
@@ -39,19 +78,46 @@ assertEquals(aceofSpades, 11); |
39 | 78 | // When the function is called with such a card, |
40 | 79 | // Then it should return the numeric value corresponding to the rank (e.g., "5" should return 5). |
41 | 80 | const fiveofHearts = getCardValue("5♥"); |
42 | | -// ====> write your test here, and then add a line to pass the test in the function above |
| 81 | +assertEquals(fiveofHearts, 5); |
43 | 82 |
|
44 | 83 | // Handle Face Cards (J, Q, K): |
45 | 84 | // Given a card with a rank of "10," "J," "Q," or "K", |
46 | 85 | // When the function is called with such a card, |
47 | 86 | // Then it should return the value 10, as these cards are worth 10 points each in blackjack. |
| 87 | +const tenOfDiamonds = getCardValue("10♦"); |
| 88 | +assertEquals(tenOfDiamonds, 10); |
| 89 | +const jackOfClubs = getCardValue("J♣"); |
| 90 | +assertEquals(jackOfClubs, 10); |
| 91 | +const queenOfSpades = getCardValue("Q♠"); |
| 92 | +assertEquals(queenOfSpades, 10); |
| 93 | +const kingOfHearts = getCardValue("K♥"); |
| 94 | +assertEquals(kingOfHearts, 10); |
48 | 95 |
|
49 | 96 | // Handle Ace (A): |
50 | 97 | // Given a card with a rank of "A", |
51 | 98 | // When the function is called with an Ace, |
52 | 99 | // Then it should, by default, assume the Ace is worth 11 points, which is a common rule in blackjack. |
| 100 | +const aceOfDiamonds = getCardValue("A♦"); |
| 101 | +assertEquals(aceOfDiamonds, 11); |
| 102 | +const aceOfClubs = getCardValue("A♣"); |
| 103 | +assertEquals(aceOfClubs, 11); |
| 104 | +const aceOfHearts = getCardValue("A♥"); |
| 105 | +assertEquals(aceOfHearts, 11); |
| 106 | +//Actually, these cases already covered in the function getCardValue() |
53 | 107 |
|
54 | 108 | // Handle Invalid Cards: |
55 | 109 | // Given a card with an invalid rank (neither a number nor a recognized face card), |
56 | 110 | // When the function is called with such a card, |
57 | 111 | // Then it should throw an error indicating "Invalid card rank." |
| 112 | +const invalidNumberCard = getCardValue("12♥"); |
| 113 | +assertEquals(invalidNumberCard, "Invalid card rank."); |
| 114 | +const invalidFaceCard = getCardValue("5*"); |
| 115 | +assertEquals(invalidFaceCard, "Invalid card rank."); |
| 116 | +const invalidFaceCard10 = getCardValue("10$"); |
| 117 | +assertEquals(invalidFaceCard10, "Invalid card rank."); |
| 118 | +const invalidFaceCardJack = getCardValue("J%"); |
| 119 | +assertEquals(invalidFaceCardJack, "Invalid card rank."); |
| 120 | +const invalidFaceCardQueen = getCardValue("Q£"); |
| 121 | +assertEquals(invalidFaceCardQueen, "Invalid card rank."); |
| 122 | +const invalidFaceCardKing = getCardValue("K^"); |
| 123 | +assertEquals(invalidFaceCardKing, "Invalid card rank."); |
0 commit comments