|
23 | 23 |
|
24 | 24 | function getCardValue(card) { |
25 | 25 | // TODO: Implement this function |
26 | | - if ( |
27 | | - card.length < 2 || |
28 | | - card.length > 3 || |
29 | | - !["♠", "♥", "♦", "♣"].includes(card.slice(-1)) |
30 | | - ) |
31 | | - throw new Error("invalid suit"); |
32 | | - if (card[0] === "A") return 11; |
33 | | - if (["J", "Q", "K"].includes(card[0])) return 10; |
34 | | - if (["2", "3", "4", "5", "6", "7", "8", "9", "10"].includes(card[0])) |
35 | | - return card[0]; // the parseint() or Number() can be used to convert the string to a number |
36 | | - throw new Error("invalid rank"); |
| 26 | + const cardRank = card.slice(0, -1); |
| 27 | + const cardSuit = card.slice(-1); |
| 28 | + if (card.length < 2 || !["♠", "♥", "♦", "♣"].includes(cardSuit)) |
| 29 | + throw new Error("invalid card suit"); |
| 30 | + if (cardRank === "A") return 11; |
| 31 | + if (["J", "Q", "K"].includes(cardRank)) return 10; |
| 32 | + if (["2", "3", "4", "5", "6", "7", "8", "9", "10"].includes(cardRank)) |
| 33 | + return cardRank; // the parseint() or Number() can be used to convert the string to a number |
| 34 | + throw new Error("invalid card rank"); |
37 | 35 | } |
38 | 36 |
|
39 | 37 | // The line below allows us to load the getCardValue function into tests in other files. |
|
0 commit comments