2020// Acceptance criteria:
2121// After you have implemented the function, write tests to cover all the cases, and
2222// execute the code to ensure all tests pass.
23-
2423function getCardValue ( card ) {
25- // TODO: Implement this function
26- if ( card === "A♠" || card === "A♥" || card === "A♦" || card === "A♣" ) {
27- return 11 ;
28- }
29- else if ( card === "J♠" || card === "J♥" || card === "J♦" || card === "J♣" || card === "Q♠" || card === "Q♥" || card === "Q♦" || card === "Q♣" || card === "K♠" || card === "K♥" || card === "K♦" || card === "K♣" ) {
30- return 10 ;
31- }
32- else if ( card === "2♠" || card === "2♥" || card === "2♦" || card === "2♣" ) {
33- return 2 ;
34- }
35- else if ( card === "3♠" || card === "3♥" || card === "3♦" || card === "3♣" ) {
36- return 3 ;
24+ if ( typeof card !== "string" ) {
25+ throw new Error ( "Invalid card" ) ;
3726 }
38- else {
27+
28+ const suits = [ "♠" , "♥" , "♦" , "♣" ] ;
29+ const suit = card . slice ( - 1 ) ;
30+ const rank = card . slice ( 0 , - 1 ) ;
31+
32+ if ( ! suits . includes ( suit ) ) {
3933 throw new Error ( "Invalid card" ) ;
4034 }
35+
36+ if ( rank === "A" ) return 11 ;
37+ if ( rank === "J" || rank === "Q" || rank === "K" ) return 10 ;
38+
39+ const num = Number ( rank ) ;
40+ if ( Number . isInteger ( num ) && num >= 2 && num <= 10 && String ( num ) === rank ) {
41+ return num ;
42+ }
43+
44+ throw new Error ( "Invalid card" ) ;
4145}
4246
43- // The line below allows us to load the getCardValue function into tests in other files.
44- // This will be useful in the "rewrite tests with jest" step.
4547module . exports = getCardValue ;
4648
4749// Helper functions to make our assertions easier to read.
@@ -72,23 +74,45 @@ assertEquals(getCardValue("Q♦"), 10);
7274assertEquals ( getCardValue ( "K♥" ) , 10 ) ;
7375assertEquals ( getCardValue ( "2♠" ) , 2 ) ;
7476assertEquals ( getCardValue ( "3♥" ) , 3 ) ;
75-
77+
7678// What other valid card cases can you think of?
77- function assertEqual ( func , valid message ) {
78- try {
79+ function assertValid ( func , description ) {
80+ try {
7981 func ( ) ;
80- console . log ( "Valid card test passed " ) ;
81- }
82+ console . log ( `Valid card test passed: ${ description } ` ) ;
83+ } catch ( e ) {
84+ console . error ( `Valid card test FAILED (threw unexpectedly): ${ description } - ${ e . message } ` ) ;
85+ }
86+ }
87+
8288// What other invalid card cases can you think of?
83- function assertThrows ( func , errorMessage ) {
89+ function assertThrows ( func , description ) {
8490 try {
8591 func ( ) ;
86- console . error ( " Error was not thrown " ) ;
92+ console . error ( ` Error was not thrown for: ${ description } ` ) ;
8793 } catch ( e ) {
88- if ( e . message === errorMessage ) {
89- console . log ( "Error thrown as expected " ) ;
90- } else {
91- console . error ( `Unexpected error message: ${ e . message } ` ) ;
92- }
94+ console . log ( `Error thrown as expected for: ${ description } ` ) ;
9395 }
94- }
96+ }
97+
98+ // What other valid card cases can you think of?
99+ assertValid ( ( ) => assertEquals ( getCardValue ( "10♦" ) , 10 ) , "10♦ (double-digit rank)" ) ;
100+ assertValid ( ( ) => assertEquals ( getCardValue ( "A♣" ) , 11 ) , "A♣" ) ;
101+ assertValid ( ( ) => assertEquals ( getCardValue ( "K♠" ) , 10 ) , "K♠" ) ;
102+ assertValid ( ( ) => assertEquals ( getCardValue ( "7♥" ) , 7 ) , "7♥" ) ;
103+ assertValid ( ( ) => assertEquals ( getCardValue ( "2♦" ) , 2 ) , "2♦" ) ;
104+
105+ // What other invalid card cases can you think of?
106+ assertThrows ( ( ) => getCardValue ( "" ) , "empty string" ) ;
107+ assertThrows ( ( ) => getCardValue ( "1♠" ) , "rank 1 doesn't exist" ) ;
108+ assertThrows ( ( ) => getCardValue ( "11♠" ) , "rank 11 doesn't exist" ) ;
109+ assertThrows ( ( ) => getCardValue ( "A" ) , "missing suit" ) ;
110+ assertThrows ( ( ) => getCardValue ( "♠" ) , "missing rank" ) ;
111+ assertThrows ( ( ) => getCardValue ( "AS" ) , "letter suit instead of emoji" ) ;
112+ assertThrows ( ( ) => getCardValue ( "a♠" ) , "lowercase rank" ) ;
113+ assertThrows ( ( ) => getCardValue ( "K♠ " ) , "trailing whitespace" ) ;
114+ assertThrows ( ( ) => getCardValue ( null ) , "null input" ) ;
115+ assertThrows ( ( ) => getCardValue ( 10 ) , "number input" ) ;
116+
117+ console . log ( "\nAll tests completed." ) ;
118+
0 commit comments