|
22 | 22 | // execute the code to ensure all tests pass. |
23 | 23 |
|
24 | 24 | function getCardValue(card) { |
25 | | - if (typeof card !== "string") { |
| 25 | + if (typeof card !== "string") { |
26 | 26 | throw new Error("Invalid card"); |
27 | 27 | } |
28 | 28 | let rank = card.slice(0, -1); // Get everything except the last character |
29 | | - let suit = card.slice(-1); // Get the last character |
30 | | - |
| 29 | + let suit = card.slice(-1); // Get the last character |
| 30 | + |
31 | 31 | const validSuits = ["♠", "♥", "♦", "♣"]; // check if suit is valid |
32 | 32 | if (!validSuits.includes(suit)) { |
33 | 33 | throw new Error("Invalid card"); |
34 | 34 | } |
35 | 35 |
|
36 | | - if (rank === "A"){ |
| 36 | + if (rank === "A") { |
37 | 37 | return 11; |
38 | | - }else if(/^[JQK]$/.test(rank)){ |
| 38 | + } else if (/^[JQK]$/.test(rank)) { |
39 | 39 | return 10; |
40 | | - }else if(rank.match(/^(10|[2-9])$/)){ |
| 40 | + } else if (rank.match(/^(10|[2-9])$/)) { |
41 | 41 | return Number(rank); |
42 | | - }else throw new Error("Invalid card"); |
| 42 | + } else throw new Error("Invalid card"); |
43 | 43 | } |
44 | 44 | // The line below allows us to load the getCardValue function into tests in other files. |
45 | 45 | // This will be useful in the "rewrite tests with jest" step. |
@@ -71,28 +71,28 @@ try { |
71 | 71 | // What other invalid card cases can you think of? |
72 | 72 |
|
73 | 73 | try { |
74 | | - getCardValue("9X"); // invalid suit |
| 74 | + getCardValue("9X"); // invalid suit |
75 | 75 | console.error('Test failed for "9X": error was not thrown'); |
76 | 76 | } catch (e) { |
77 | 77 | console.log('Test passed for "9X": caught error ->', e.message); |
78 | 78 | } |
79 | 79 |
|
80 | 80 | try { |
81 | | - getCardValue("1♠"); // invalid rank |
| 81 | + getCardValue("1♠"); // invalid rank |
82 | 82 | console.error('Test failed for "1♠": error was not thrown'); |
83 | 83 | } catch (e) { |
84 | 84 | console.log('Test passed for "1♠": caught error ->', e.message); |
85 | 85 | } |
86 | 86 |
|
87 | 87 | try { |
88 | | - getCardValue("0♥"); // invalid rank |
| 88 | + getCardValue("0♥"); // invalid rank |
89 | 89 | console.error('Test failed for "0♥": error was not thrown'); |
90 | 90 | } catch (e) { |
91 | 91 | console.log('Test passed for "0♥": caught error ->', e.message); |
92 | 92 | } |
93 | 93 |
|
94 | 94 | try { |
95 | | - getCardValue("ABC"); // completely wrong format |
| 95 | + getCardValue("ABC"); // completely wrong format |
96 | 96 | console.error('Test failed for "ABC": error was not thrown'); |
97 | 97 | } catch (e) { |
98 | 98 | console.log('Test passed for "ABC": caught error ->', e.message); |
|
0 commit comments