|
22 | 22 | // execute the code to ensure all tests pass. |
23 | 23 |
|
24 | 24 | function getCardValue(card) { |
| 25 | + const ranks = [ |
| 26 | + "A", |
| 27 | + "2", |
| 28 | + "3", |
| 29 | + "4", |
| 30 | + "5", |
| 31 | + "6", |
| 32 | + "7", |
| 33 | + "8", |
| 34 | + "9", |
| 35 | + "10", |
| 36 | + "J", |
| 37 | + "Q", |
| 38 | + "K", |
| 39 | + ]; |
| 40 | + const rankNum = ["2", "3", "4", "5", "6", "7", "8", "9", "10"]; |
| 41 | + const rankFace = ["J", "Q", "K"]; |
25 | 42 | const suits = ["♠", "♥", "♦", "♣"]; |
26 | | - if (card[0] === "A" && suits.includes(card[1]) && card.length === 2) |
27 | | - return 11; |
28 | | - if ( |
29 | | - (card[0] === "J" || card[0] === "Q" || card[0] === "K") && |
30 | | - suits.includes(card[1]) && |
31 | | - card.length === 2 |
32 | | - ) |
33 | | - return 10; |
34 | | - if ( |
35 | | - ["2", "3", "4", "5", "6", "7", "8", "9"].includes(card[0]) && |
36 | | - suits.includes(card[1]) && |
37 | | - card.length === 2 |
38 | | - ) |
39 | | - return Number(card[0]); |
40 | | - if (card.slice(0, 2) == "10" && suits.includes(card[2]) && card.length === 3) |
41 | | - return 10; |
| 43 | + |
| 44 | + const rank = card.slice(0, -1); |
| 45 | + const suit = card.slice(-1); |
| 46 | + |
| 47 | + if (suits.includes(suit) && ranks.includes(rank)) { |
| 48 | + if (rank === "A") { |
| 49 | + return 11; |
| 50 | + } |
| 51 | + if (rankNum.includes(rank)) { |
| 52 | + return Number(rank); |
| 53 | + } |
| 54 | + if (rankFace.includes(rank)) { |
| 55 | + return 10; |
| 56 | + } |
| 57 | + } |
| 58 | + |
42 | 59 | throw new Error( |
43 | 60 | 'Invalid input: Expected rank followed by a suit symbol. For example: "A♠", "2♥", "10♥", "J♣", "Q♦", "K♦"' |
44 | 61 | ); |
|
0 commit comments