2222// execute the code to ensure all tests pass.
2323
2424function getCardValue ( card ) {
25- const validSuits = [ "♠" , "♥" , "♦" , "♣" ] ;
26- const validRanks = [ "A" , "2" , "3" , "4" , "5" , "6" , "7" , "8" , "9" , "10" , "J" , "Q" , "K" ] ;
27-
28- const suit = card . slice ( - 1 ) ;
29- const rank = card . slice ( 0 , - 1 ) ;
30-
3125
32- if ( ! validSuits . includes ( suit ) || ! validRanks . includes ( rank ) ) {
33- throw new Error ( "Invalid card" ) ;
34- }
35-
36- if ( rank === "A" ) return 11 ;
37- if ( [ "J" , "Q" , "K" ] . includes ( rank ) ) return 10 ;
38-
39- return Number ( rank ) ;
4026}
4127
4228
@@ -56,28 +42,6 @@ function assertEquals(actualOutput, targetOutput) {
5642// Examples 1 numbers:
5743// Examples 1 numbers:
5844assertEquals ( getCardValue ( "9♠" ) , 9 ) ;
59- assertEquals ( getCardValue ( "5♣" ) , 5 ) ;
60-
61- //Examples A:
62- assertEquals ( getCardValue ( "A♦" ) , 11 )
63-
64- //Examples J Q K:,
65- assertEquals ( getCardValue ( "Q♥" ) , 10 )
66- assertEquals ( getCardValue ( "K♥" ) , 10 )
67-
68- //Examples invalid numbers:
69- assertThrow ( ( ) => getCardValue ( "1♥" ) )
70- assertThrow ( ( ) => getCardValue ( "11♥" ) )
71-
72- //Examples invalid suit:
73- assertThrow ( ( ) => getCardValue ( "A*" ) )
74-
75- //Examples missing rank or suit:
76- assertThrow ( ( ) => getCardValue ( "♥" ) )
77- assertThrow ( ( ) => getCardValue ( "5" ) )
78-
79- //Example extra suit:
80- assertThrow ( ( ) => getCardValue ( "Q♠♠" ) )
8145
8246
8347function assertThrow ( fn ) {
@@ -88,9 +52,7 @@ try { (fn)
8852 console . error ( "Error was not thrown for invalid card" ) ;
8953} catch ( error ) {
9054 // if the above code throws, we catch the error here, that stops the whole program crashing
91- console . log ( 'there was an error getting card value!' , error )
92- } }
55+
9356
94- console . log ( ' we finished running getcardvalue' )
9557
9658// What other invalid card cases can you think of?
0 commit comments