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- if ( rank === "A" ) {
12- return 11 ;
11+ const rank = card . slice ( 0 , - 1 )
12+ const suit = card . slice ( - 1 )
13+ if ( ! [ "♠" , "♥" , "♦" , "♣" ] . includes ( suit ) ) {
14+ return "Invalid card rank." ;
1315 }
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+ }
36+
1437}
1538
1639// The line below allows us to load the getCardValue function into tests in other files.
@@ -40,18 +63,36 @@ assertEquals(aceofSpades, 11);
4063// Then it should return the numeric value corresponding to the rank (e.g., "5" should return 5).
4164const fiveofHearts = getCardValue ( "5♥" ) ;
4265// ====> write your test here, and then add a line to pass the test in the function above
43-
66+ assertEquals ( fiveofHearts , 5 ) ;
4467// Handle Face Cards (J, Q, K):
4568// Given a card with a rank of "10," "J," "Q," or "K",
4669// When the function is called with such a card,
4770// Then it should return the value 10, as these cards are worth 10 points each in blackjack.
71+ const kingofDiamonds = getCardValue ( "K♦" ) ;
72+ assertEquals ( kingofDiamonds , 10 ) ;
73+
74+ const tenofClubs = getCardValue ( "10♣" ) ;
75+ assertEquals ( tenofClubs , 10 ) ;
4876
4977// Handle Ace (A):
5078// Given a card with a rank of "A",
5179// When the function is called with an Ace,
5280// Then it should, by default, assume the Ace is worth 11 points, which is a common rule in blackjack.
53-
81+ const aceofHearts = getCardValue ( "A♥" ) ;
82+ assertEquals ( aceofHearts , 11 ) ;
5483// Handle Invalid Cards:
5584// Given a card with an invalid rank (neither a number nor a recognized face card),
5685// When the function is called with such a card,
5786// Then it should throw an error indicating "Invalid card rank."
87+
88+ const invalidCard = getCardValue ( "1♠" ) ;
89+ assertEquals ( invalidCard , "Invalid card rank." ) ;
90+
91+ const invalidCard2 = getCardValue ( "B♦" ) ;
92+ assertEquals ( invalidCard2 , "Invalid card rank." ) ;
93+
94+ const invalidCard3 = getCardValue ( "11" ) ;
95+ assertEquals ( invalidCard3 , "Invalid card rank." ) ;
96+
97+ const invalidCard4 = getCardValue ( "3😄" ) ;
98+ assertEquals ( invalidCard4 , "Invalid card rank." ) ;
0 commit comments