|
23 | 23 |
|
24 | 24 | function getCardValue(card) { |
25 | 25 | // TODO: Implement this function |
| 26 | + |
| 27 | + // 1. Define valid suits and ranks |
| 28 | + const suits = ["♠", "♥", "♦", "♣"]; |
| 29 | + const ranks = [ |
| 30 | + "A", |
| 31 | + "2", |
| 32 | + "3", |
| 33 | + "4", |
| 34 | + "5", |
| 35 | + "6", |
| 36 | + "7", |
| 37 | + "8", |
| 38 | + "9", |
| 39 | + "10", |
| 40 | + "J", |
| 41 | + "Q", |
| 42 | + "K", |
| 43 | + ]; |
| 44 | + |
| 45 | + // 2. Basic validation: ensure card is a string and not empty |
| 46 | + if (typeof card !== "string" || card.length === 0) { |
| 47 | + throw new Error("Card must be a non-empty string"); |
| 48 | + } |
| 49 | + |
| 50 | + // 3. Extract rank and suit from the card string |
| 51 | + const rank = card.substring(0, card.length - 1); |
| 52 | + const suit = card.substring(card.length - 1); |
| 53 | + |
| 54 | + // 4. Validate rank and suit |
| 55 | + if (!ranks.includes(rank)) { |
| 56 | + throw new Error("Invalid rank"); |
| 57 | + } |
| 58 | + if (!suits.includes(suit)) { |
| 59 | + throw new Error("Invalid suit"); |
| 60 | + } |
| 61 | + |
| 62 | + // 5. Return the appropriate value based on the rank |
| 63 | + if (rank === "A") { |
| 64 | + return 11; |
| 65 | + } else if (["J", "Q", "K"].includes(rank)) { |
| 66 | + return 10; |
| 67 | + } else { |
| 68 | + return parseInt(rank); |
| 69 | + } |
26 | 70 | } |
27 | 71 |
|
28 | 72 | // The line below allows us to load the getCardValue function into tests in other files. |
|
50 | 94 | } catch (e) {} |
51 | 95 |
|
52 | 96 | // What other invalid card cases can you think of? |
| 97 | + |
| 98 | +try { |
| 99 | + getCardValue("11♠"); |
| 100 | + console.error("Error was not thrown for invalid rank"); |
| 101 | +} catch (e) {} |
| 102 | + |
| 103 | +try { |
| 104 | + getCardValue("A♤"); |
| 105 | + console.error("Error was not thrown for invalid suit"); |
| 106 | +} catch (e) {} |
| 107 | + |
| 108 | +try { |
| 109 | + getCardValue(""); |
| 110 | + console.error("Error was not thrown for empty string"); |
| 111 | +} catch (e) {} |
| 112 | + |
| 113 | +try { |
| 114 | + getCardValue(123); |
| 115 | + console.error("Error was not thrown for non-string input"); |
| 116 | +} catch (e) {} |
| 117 | + |
| 118 | +try { |
| 119 | + getCardValue("10"); |
| 120 | + console.error("Error was not thrown for missing suit"); |
| 121 | +} catch (e) {} |
| 122 | + |
| 123 | +try { |
| 124 | + getCardValue("♠"); |
| 125 | + console.error("Error was not thrown for missing rank"); |
| 126 | +} catch (e) {} |
| 127 | + |
| 128 | +try { |
| 129 | + getCardValue("A"); |
| 130 | + console.error("Error was not thrown for missing suit"); |
| 131 | +} catch (e) {} |
| 132 | + |
| 133 | +try { |
| 134 | + getCardValue("1♠"); |
| 135 | + console.error("Error was not thrown for invalid rank"); |
| 136 | +} catch (e) {} |
| 137 | + |
| 138 | +try { |
| 139 | + getCardValue("10XX"); |
| 140 | + console.error("Error was not thrown for extra characters"); |
| 141 | +} catch (e) {} |
| 142 | + |
| 143 | +try { |
| 144 | + getCardValue("Z♦"); |
| 145 | + console.error("Error was not thrown for invalid rank"); |
| 146 | +} catch (e) {} |
0 commit comments