2222// execute the code to ensure all tests pass.
2323
2424function getCardValue ( card ) {
25- // TODO: Implement this function
25+ const suit = card . replaceAll ( / [ \w \d \s ] / g, "" ) ;
26+ const value = card . replaceAll ( / [ ^ \w \d \s ] / g, "" ) ;
27+ const face = [ "J" , "Q" , "K" ] ;
28+ const validSuits = [ "♠" , "♥" , "♦" , "♣" ] ;
29+ const validValue = [
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+ if (
46+ ! validSuits . includes ( suit ) ||
47+ ! validValue . includes ( value ) ||
48+ card !== value + suit
49+ ) {
50+ throw new Error ( "Invalid card" ) ;
51+ }
52+
53+ if ( validSuits . includes ( suit ) ) {
54+ switch ( true ) {
55+ case Number ( value ) <= 10 && Number ( value ) >= 2 :
56+ return Number ( value ) ;
57+ case value === "A" :
58+ return 11 ;
59+ case face . includes ( value ) :
60+ return 10 ;
61+ }
62+ }
63+
64+ return "Invalid card" ;
2665}
2766
2867// The line below allows us to load the getCardValue function into tests in other files.
@@ -39,7 +78,23 @@ function assertEquals(actualOutput, targetOutput) {
3978
4079// TODO: Write tests to cover all outcomes, including throwing errors for invalid cards.
4180// Examples:
42- assertEquals ( getCardValue ( "9♠" ) , 9 ) ;
81+
82+ // Test number cards
83+ assertEquals ( getCardValue ( "2♠" ) , 2 ) ;
84+ assertEquals ( getCardValue ( "5♥" ) , 5 ) ;
85+ assertEquals ( getCardValue ( "9♦" ) , 9 ) ;
86+ assertEquals ( getCardValue ( "10♣" ) , 10 ) ;
87+
88+ // Test aces
89+ assertEquals ( getCardValue ( "A♠" ) , 11 ) ;
90+ assertEquals ( getCardValue ( "A♥" ) , 11 ) ;
91+ assertEquals ( getCardValue ( "A♦" ) , 11 ) ;
92+ assertEquals ( getCardValue ( "A♣" ) , 11 ) ;
93+
94+ // Test face cards
95+ assertEquals ( getCardValue ( "J♠" ) , 10 ) ;
96+ assertEquals ( getCardValue ( "Q♥" ) , 10 ) ;
97+ assertEquals ( getCardValue ( "K♦" ) , 10 ) ;
4398
4499// Handling invalid cards
45100try {
@@ -52,3 +107,43 @@ try {
52107}
53108
54109// What other invalid card cases can you think of?
110+
111+ function errorThrown ( card ) {
112+ try {
113+ getCardValue ( card ) ;
114+ console . error ( `Expected "${ card } " to throw an error 😢` ) ;
115+ } catch ( error ) {
116+ console . log ( `"${ card } " Error thrown for invalid card 🎉` ) ;
117+ }
118+ }
119+
120+ // Missing a suit
121+ errorThrown ( "9" ) ;
122+
123+ // Missing a rank
124+ errorThrown ( "♠" ) ;
125+
126+ // Invalid rank
127+ errorThrown ( "1♠" ) ;
128+ errorThrown ( "11♠" ) ;
129+ errorThrown ( "Z♠" ) ;
130+
131+ // Invalid suit
132+ errorThrown ( "9X" ) ;
133+
134+ // Suit and rank in the wrong order
135+ errorThrown ( "♠9" ) ;
136+
137+ // Extra characters
138+ errorThrown ( "9♠hello" ) ;
139+ errorThrown ( "A♠♠" ) ;
140+
141+ // Spaces
142+ errorThrown ( "9 ♠" ) ;
143+ errorThrown ( " 9♠" ) ;
144+ errorThrown ( "9♠ " ) ;
145+
146+ // Empty or incorrectly capitalised
147+ errorThrown ( "" ) ;
148+ errorThrown ( "a♠" ) ;
149+ errorThrown ( "j♠" ) ;
0 commit comments