2323
2424function getCardValue ( card ) {
2525 // TODO: Implement this function
26+ // Checks that input is a string and has correct length,
27+ // 2 chars for 2-9/A/J/Q/K, 3 chars for 10
28+ if ( typeof card !== 'string' || ( card . length !== 2 && card . length !== 3 ) ) {
29+ throw new Error ( 'Invalid card format' ) ;
30+ }
31+ // Get the suit, always the last character
32+ const suit = card . slice ( - 1 ) ;
33+ // Get the rank, everything except the last character
34+ const rank = card . slice ( 0 , - 1 ) ;
35+ // Checks if suit is one of the four allowed symbols
36+ if ( ! '♠♥♦♣' . includes ( suit ) ) {
37+ throw new Error ( 'Invalid suit' ) ;
38+ }
39+ // Special case: Ace is worth 11
40+ if ( rank === 'A' ) return 11 ;
41+ // Special case: Face cards (Jack, Queen, King) are worth 10
42+ if ( rank === 'J' || rank === 'Q' || rank === 'K' ) return 10 ;
43+ // For number cards (2-10): converts rank string into number
44+ const value = Number ( rank ) ;
45+ // Makes sure it's actually a valid number between 2 and 10
46+ if ( isNaN ( value ) || value < 2 || value > 10 ) {
47+ throw new Error ( 'Invalid rank' ) ;
48+ }
49+ // Return the numeric value for 2-10
50+ return value ;
2651}
2752
2853// The line below allows us to load the getCardValue function into tests in other files.
@@ -41,7 +66,29 @@ function assertEquals(actualOutput, targetOutput) {
4166// Examples:
4267assertEquals ( getCardValue ( "9♠" ) , 9 ) ;
4368
69+ // Valid number cards (2-10)
70+ const twoofHearts = getCardValue ( "2♥" ) ;
71+ assertEquals ( twoofHearts , 2 ) ;
72+
73+ const fiveofDiamonds = getCardValue ( "5♦" ) ;
74+ assertEquals ( fiveofDiamonds , 5 ) ;
75+
76+ // Ace returns 11
77+ const aceofSpades = getCardValue ( "A♠" ) ;
78+ assertEquals ( aceofSpades , 11 ) ;
79+
80+ // Face cards return 10 (J, Q, K)
81+ const jackofHearts = getCardValue ( "J♥" ) ;
82+ assertEquals ( jackofHearts , 10 ) ;
83+
84+ const queenofDiamonds = getCardValue ( "Q♦" ) ;
85+ assertEquals ( queenofDiamonds , 10 ) ;
86+
87+ const kingofClubs = getCardValue ( "K♣" ) ;
88+ assertEquals ( kingofClubs , 10 ) ;
89+
4490// Handling invalid cards
91+ // Invalid format, random string
4592try {
4693 getCardValue ( "invalid" ) ;
4794
5097} catch ( e ) { }
5198
5299// What other invalid card cases can you think of?
100+ // Too short, missing suit
101+ try {
102+ getCardValue ( "A" ) ;
103+ console . error ( "Error was not thrown for too short card" ) ;
104+ } catch ( e ) { }
105+
106+ // Invalid rank
107+ try {
108+ getCardValue ( "X♠" ) ;
109+ console . error ( "Error was not thrown for invalid rank" ) ;
110+ } catch ( e ) { }
111+
112+ // Invalid suit, wrong symbol
113+ try {
114+ getCardValue ( "Kx" ) ;
115+ console . error ( "Error was not thrown for missing suit" ) ;
116+ } catch ( e ) { }
117+
118+ // Missing suit
119+ try {
120+ getCardValue ( "10" ) ;
121+ console . error ( "Error was not thrown for missing suit" ) ;
122+ } catch ( e ) { }
123+
124+ // Empty string
125+ try {
126+ getCardValue ( "" ) ;
127+ console . error ( "Error was not thrown for empty string" ) ;
128+ } catch ( e ) { }
0 commit comments