|
24 | 24 | // execute the code to ensure all tests pass. |
25 | 25 |
|
26 | 26 | export function getCardValue(card) { |
| 27 | + const rank = card.slice(0, -1); |
27 | 28 | const suit = card.slice(-1); |
28 | | - if (!["♠", "♥", "♦", "♣"].includes(suit)) { |
29 | | - throw new Error(`Expected a number followed by a suit, but got "${card}"`); |
| 29 | + |
| 30 | + const validSuits = ["♠", "♥", "♦", "♣"]; |
| 31 | + const numberRanks = ["2", "3", "4", "5", "6", "7", "8", "9", "10"]; |
| 32 | + const faceRanks = ["J", "Q", "K"]; |
| 33 | + |
| 34 | + if (!validSuits.includes(suit)) { |
| 35 | + throw new Error(`Expected a rank followed by a suit, but got "${card}"`); |
30 | 36 | } |
31 | | - if (card.slice(0, -1) === "A") { |
| 37 | + |
| 38 | + if (rank === "A") { |
32 | 39 | return 11; |
33 | | - } |
34 | | - if (card === "9♠") { |
35 | | - return 9; |
36 | | - } else if (card === "J♠" || card === "Q♥" || card === "K♦") { |
| 40 | + } else if (faceRanks.includes(rank)) { |
37 | 41 | return 10; |
38 | | - } else if ( |
39 | | - Number(card.slice(0, -1) >= 2 && Number(card.slice(0, -1) <= 10)) |
40 | | - ) { |
41 | | - return Number(card.slice(0, -1)); |
42 | | - } else { |
43 | | - throw new Error(`Expected a number followed by a suit, but got "${card}"`); |
| 42 | + } else if (numberRanks.includes(rank)) { |
| 43 | + return Number(rank); |
44 | 44 | } |
| 45 | + |
| 46 | + throw new Error(`Expected a rank followed by a suit, but got "${card}"`); |
45 | 47 | } |
0 commit comments