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
@@ -55,28 +41,6 @@ function assertEquals(actualOutput, targetOutput) {
5541// TODO: Write tests to cover all outcomes, including throwing errors for invalid cards.
5642// Examples 1 numbers:
5743assertEquals ( getCardValue ( "9♠" ) , 9 ) ;
58- assertEquals ( getCardValue ( "5♣" ) , 5 ) ;
59-
60- //Examples A:
61- assertEquals ( getCardValue ( "A♦" ) , 11 )
62-
63- //Examples J Q K:,
64- assertEquals ( getCardValue ( "Q♥" ) , 10 )
65- assertEquals ( getCardValue ( "K♥" ) , 10 )
66-
67- //Examples invalid numbers:
68- assertThrow ( ( ) => getCardValue ( "1♥" ) )
69- assertThrow ( ( ) => getCardValue ( "11♥" ) )
70-
71- //Examples invalid suit:
72- assertThrow ( ( ) => getCardValue ( "A*" ) )
73-
74- //Examples missing rank or suit:
75- assertThrow ( ( ) => getCardValue ( "♥" ) )
76- assertThrow ( ( ) => getCardValue ( "5" ) )
77-
78- //Example extra suit:
79- assertThrow ( ( ) => getCardValue ( "Q♠♠" ) )
8044
8145
8246function assertThrow ( fn ) {
@@ -87,10 +51,8 @@ try { (fn)
8751 console . error ( "Error was not thrown for invalid card" ) ;
8852} catch ( error ) {
8953 // if the above code throws, we catch the error here, that stops the whole program crashing
90- console . log ( 'there was an error getting card value!' , error )
91- } }
54+
9255
93- console . log ( ' we finished running getcardvalue' )
9456
9557// What other invalid card cases can you think of?
9658
0 commit comments