|
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); |
| 11 | + const rank = card.length > 1 ? card.slice(0, -1) : card; |
12 | 12 |
|
13 | 13 | if (rank === "A") { |
14 | 14 | return 11; |
15 | 15 | } else if (["J", "Q", "K"].includes(rank)) { |
16 | 16 | return 10; |
17 | | - } else if (rank >= 2 && rank <= 10) { |
| 17 | + } else if (/^(?:[2-9]|10)$/.test(rank)) { |
18 | 18 | return Number(rank); |
19 | 19 | } else { |
20 | 20 | return new Error("Invalid card rank."); |
@@ -67,5 +67,13 @@ assertEquals(ace, 11); |
67 | 67 | // Given a card with an invalid rank (neither a number nor a recognized face card), |
68 | 68 | // When the function is called with such a card, |
69 | 69 | // Then it should throw an error indicating "Invalid card rank." |
| 70 | + |
| 71 | +// assertEquals() doesn’t handle an actual Error being thrown, so the console will display: |
| 72 | +// |
| 73 | +// Expected Error: Invalid card rank. to equal Invalid card rank. |
| 74 | +// 3-get-card-value.js:32 |
| 75 | +// Assertion failed: Expected Error: Invalid card rank. to equal Invalid card rank. |
| 76 | +// |
| 77 | +// It will work if getCardValue() returns a string instead. |
70 | 78 | const invalidRank = getCardValue("@"); |
71 | 79 | assertEquals(invalidRank, "Invalid card rank."); |
0 commit comments