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 ) ; // remove the suit symbol
12+
1113 if ( rank === "A" ) {
1214 return 11 ;
1315 }
16+
17+ if ( [ "K" , "Q" , "J" , "10" ] . includes ( rank ) ) {
18+ return 10 ;
19+ }
20+
21+ const numeric = parseInt ( rank ) ;
22+ if ( ! isNaN ( numeric ) ) {
23+ return numeric ;
24+ }
25+
26+ throw new Error ( "Invalid card rank." ) ;
1427}
1528
1629// The line below allows us to load the getCardValue function into tests in other files.
@@ -39,6 +52,7 @@ assertEquals(aceofSpades, 11);
3952// When the function is called with such a card,
4053// Then it should return the numeric value corresponding to the rank (e.g., "5" should return 5).
4154const fiveofHearts = getCardValue ( "5♥" ) ;
55+ assertEquals ( fiveofHearts , 5 ) ;
4256// ====> write your test here, and then add a line to pass the test in the function above
4357
4458// Handle Face Cards (J, Q, K):
@@ -55,3 +69,26 @@ const fiveofHearts = getCardValue("5♥");
5569// Given a card with an invalid rank (neither a number nor a recognized face card),
5670// When the function is called with such a card,
5771// Then it should throw an error indicating "Invalid card rank."
72+ //Stretch (personal)
73+ // Handle Face Cards (J, Q, K):
74+ const kingOfClubs = getCardValue ( "K♣" ) ;
75+ assertEquals ( kingOfClubs , 10 ) ;
76+
77+ const queenOfDiamonds = getCardValue ( "Q♦" ) ;
78+ assertEquals ( queenOfDiamonds , 10 ) ;
79+
80+ const jackOfSpades = getCardValue ( "J♠" ) ;
81+ assertEquals ( jackOfSpades , 10 ) ;
82+
83+ const tenOfHearts = getCardValue ( "10♥" ) ;
84+ assertEquals ( tenOfHearts , 10 ) ;
85+ // Handle Invalid Cards:
86+ try {
87+ getCardValue ( "Z♠" ) ;
88+ console . assert ( false , "Expected an error for invalid card rank" ) ;
89+ } catch ( error ) {
90+ console . assert (
91+ error . message === "Invalid card rank." ,
92+ `Expected error message "Invalid card rank.", got "${ error . message } "`
93+ ) ;
94+ }
0 commit comments