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 ) ;
1112 if ( rank === "A" ) return 11 ;
13+ if ( rank === "J" || rank === "Q" || rank === "K" || rank === "10" ) return 10 ;
14+ if ( ! isNaN ( rank ) ) return Number ( rank ) ;
15+ throw new Error ( "Invalid card rank." ) ;
1216}
1317
1418// You need to write assertions for your function to check it works in different cases
@@ -33,12 +37,23 @@ assertEquals(aceofSpades, 11);
3337// When the function is called with such a card,
3438// Then it should return the numeric value corresponding to the rank (e.g., "5" should return 5).
3539const fiveofHearts = getCardValue ( "5♥" ) ;
36- // ====> write your test here, and then add a line to pass the test in the function above
40+ assertEquals ( fiveofHearts , 5 ) ;
3741
3842// Handle Face Cards (J, Q, K):
3943// Given a card with a rank of "10," "J," "Q," or "K",
4044// When the function is called with such a card,
4145// Then it should return the value 10, as these cards are worth 10 points each in blackjack.
46+ const jackofClubs = getCardValue ( "J♣" ) ;
47+ assertEquals ( jackofClubs , 10 ) ;
48+
49+ const queenofDiamonds = getCardValue ( "Q♦" ) ;
50+ assertEquals ( queenofDiamonds , 10 ) ;
51+
52+ const kingofHearts = getCardValue ( "K♥" ) ;
53+ assertEquals ( kingofHearts , 10 ) ;
54+
55+ const tenofSpades = getCardValue ( "10♠" ) ;
56+ assertEquals ( tenofSpades , 10 ) ;
4257
4358// Handle Ace (A):
4459// Given a card with a rank of "A",
@@ -49,3 +64,9 @@ const fiveofHearts = getCardValue("5♥");
4964// Given a card with an invalid rank (neither a number nor a recognized face card),
5065// When the function is called with such a card,
5166// Then it should throw an error indicating "Invalid card rank."
67+ try {
68+ getCardValue ( "Z♠" ) ;
69+ console . error ( "Error was expected but not thrown" ) ;
70+ } catch ( e ) {
71+ assertEquals ( e . message , "Invalid card rank." ) ;
72+ }
0 commit comments