88// write one test at a time, and make it pass, build your solution up methodically
99// just make one change at a time -- don't rush -- programmers are deep and careful thinkers
1010function getCardValue ( card ) {
11- const rank = card . slice ( 0 , - 1 )
12- const suit = card . slice ( - 1 )
13- if ( ! [ "♠" , "♥" , "♦" , "♣" ] . includes ( suit ) ) {
14- return "Invalid card rank." ;
11+ console . log ( card ) ;
12+ const rank = card . slice ( 0 , - 1 ) ;
13+ const suit = card . slice ( - 1 ) ;
14+ const suits = [ "♠" , "♥" , "♦" , "♣" ] ;
15+ const ranks = [
16+ "2" ,
17+ "3" ,
18+ "4" ,
19+ "5" ,
20+ "6" ,
21+ "7" ,
22+ "8" ,
23+ "9" ,
24+ "10" ,
25+ "J" ,
26+ "Q" ,
27+ "K" ,
28+ "A" ,
29+ ] ;
30+ if ( ! suits . includes ( suit ) || ! ranks . includes ( rank ) ) {
31+ throw new Error ( "Invalid card rank." ) ;
1532 }
16- switch ( rank ) {
17- case "2" :
18- case "3" :
19- case "4" :
20- case "5" :
21- case "6" :
22- case "7" :
23- case "8" :
24- case "9" :
25- return parseInt ( rank ) ;
26- case "10" :
27- case "J" :
28- case "Q" :
29- case "K" :
30- return 10 ;
31- case "A" :
32- return 11 ;
33- default :
34- return "Invalid card rank." ;
35- }
3633
34+ switch ( rank ) {
35+ case "1" :
36+ case "2" :
37+ case "3" :
38+ case "4" :
39+ case "5" :
40+ case "6" :
41+ case "7" :
42+ case "8" :
43+ case "9" :
44+ return parseInt ( rank ) ;
45+ case "10" :
46+ case "J" :
47+ case "Q" :
48+ case "K" :
49+ return 10 ;
50+ case "A" :
51+ return 11 ;
52+ }
3753}
3854
3955// The line below allows us to load the getCardValue function into tests in other files.
@@ -71,7 +87,7 @@ assertEquals(fiveofHearts, 5);
7187const kingofDiamonds = getCardValue ( "K♦" ) ;
7288assertEquals ( kingofDiamonds , 10 ) ;
7389
74- const tenofClubs = getCardValue ( "10♣" ) ;
90+ const tenofClubs = getCardValue ( "10♣" ) ;
7591assertEquals ( tenofClubs , 10 ) ;
7692
7793// Handle Ace (A):
@@ -85,14 +101,14 @@ assertEquals(aceofHearts, 11);
85101// When the function is called with such a card,
86102// Then it should throw an error indicating "Invalid card rank."
87103
88- const invalidCard = getCardValue ( "1♠" ) ;
104+ const invalidCard = getCardValue ( "1♠" ) ;
89105assertEquals ( invalidCard , "Invalid card rank." ) ;
90106
91- const invalidCard2 = getCardValue ( "B♦" ) ;
107+ const invalidCard2 = getCardValue ( "B♦" ) ;
92108assertEquals ( invalidCard2 , "Invalid card rank." ) ;
93109
94- const invalidCard3 = getCardValue ( "11" ) ;
110+ const invalidCard3 = getCardValue ( "11" ) ;
95111assertEquals ( invalidCard3 , "Invalid card rank." ) ;
96112
97- const invalidCard4 = getCardValue ( "3😄" ) ;
98- assertEquals ( invalidCard4 , "Invalid card rank." ) ;
113+ const invalidCard4 = getCardValue ( "3😄" ) ;
114+ assertEquals ( invalidCard4 , "Invalid card rank." ) ;
0 commit comments