|
24 | 24 | function getCardValue(card) { |
25 | 25 | const cardNum = card.slice(0, -1) |
26 | 26 | const cardSuit = card.slice(-1) |
27 | | - if (cardNum === "A" && "♠♥♦♣".includes(cardSuit)) { return 11} |
28 | | - else if (["J", "Q", "K"].includes(cardNum)) { return 10} |
29 | | - else if (Number(cardNum) >= 2 && Number(cardNum) <= 10) {return Number(cardNum)} |
| 27 | + if (cardNum === "A" && "♠♥♦♣".includes(cardSuit)) { |
| 28 | + return 11 |
| 29 | + } |
| 30 | + else if (["J", "Q", "K"].includes(cardNum) && "♠♥♦♣".includes(cardSuit)) { |
| 31 | + return 10 |
| 32 | + } |
| 33 | + else if (Number(cardNum) >= 2 && Number(cardNum) <= 10 && "♠♥♦♣".includes(cardSuit)) { |
| 34 | + return Number(cardNum) |
| 35 | + } |
30 | 36 | else throw new Error("Invalid card"); |
31 | 37 | } |
32 | 38 |
|
@@ -57,24 +63,50 @@ try { |
57 | 63 |
|
58 | 64 | // This line will not be reached if an error is thrown as expected |
59 | 65 | console.error("Error was not thrown for invalid card"); |
60 | | -} catch (e) {} |
| 66 | +} catch (e) { |
| 67 | + assertEquals(e.message, "Invalid card") |
| 68 | +} |
61 | 69 | try { |
62 | 70 | getCardValue("♦Q"); |
63 | 71 |
|
64 | 72 | // This line will not be reached if an error is thrown as expected |
65 | 73 | console.error("Error was not thrown for invalid card"); |
66 | | -} catch (e) {} |
| 74 | +} catch (e) { |
| 75 | + assertEquals(e.message, "Invalid card") |
| 76 | +} |
67 | 77 | try { |
68 | 78 | getCardValue("11♦"); |
69 | 79 |
|
70 | 80 | // This line will not be reached if an error is thrown as expected |
71 | 81 | console.error("Error was not thrown for invalid card"); |
72 | | -} catch (e) {} |
| 82 | +} catch (e) { |
| 83 | + assertEquals(e.message, "Invalid card") |
| 84 | +} |
73 | 85 |
|
74 | 86 | try { |
75 | 87 | getCardValue("AX"); |
76 | 88 |
|
77 | 89 | // This line will not be reached if an error is thrown as expected |
78 | 90 | console.error("Error was not thrown for invalid card"); |
79 | | -} catch (e) {} |
| 91 | +} catch (e) { |
| 92 | + assertEquals(e.message, "Invalid card") |
| 93 | +} |
| 94 | + |
| 95 | +try { |
| 96 | + getCardValue("KX"); |
| 97 | + |
| 98 | + // This line will not be reached if an error is thrown as expected |
| 99 | + console.error("Error was not thrown for invalid card"); |
| 100 | +} catch (e) { |
| 101 | + assertEquals(e.message, "Invalid card") |
| 102 | +} |
| 103 | + |
| 104 | +try { |
| 105 | + getCardValue("5X"); |
| 106 | + |
| 107 | + // This line will not be reached if an error is thrown as expected |
| 108 | + console.error("Error was not thrown for invalid card"); |
| 109 | +} catch (e) { |
| 110 | + assertEquals(e.message, "Invalid card") |
| 111 | +} |
80 | 112 | // What other invalid card cases can you think of? |
0 commit comments