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+
12+ const rank = card . slice ( 0 , - 1 ) ;
1113 if ( rank === "A" ) {
1214 return 11 ;
1315 }
16+ else if ( [ "K" , "J" , "Q" , "10" ] . includes ( rank ) ) {
17+ return 10 ;
18+ }
19+ else if ( ! isNaN ( rank ) ) {
20+ return Number ( rank ) ;
21+ }
22+ else {
23+ throw new Error ( "Invalid Card" ) ;
24+ }
1425}
1526
1627// The line below allows us to load the getCardValue function into tests in other files.
@@ -41,19 +52,38 @@ assertEquals(aceofSpades, 11);
4152// When the function is called with such a card,
4253// Then it should return the numeric value corresponding to the rank (e.g., "5" should return 5).
4354const fiveofHearts = getCardValue ( "5♥" ) ;
55+ assertEquals ( fiveofHearts , 5 ) ;
4456// ====> write your test here, and then add a line to pass the test in the function above
4557
58+
59+
4660// Handle Face Cards (J, Q, K):
4761// Given a card with a rank of "10," "J," "Q," or "K",
4862// When the function is called with such a card,
4963// Then it should return the value 10, as these cards are worth 10 points each in blackjack.
5064
65+ const faceCards = getCardValue ( "J♥" ) ;
66+ assertEquals ( faceCards , 10 ) ;
67+
68+ const tenOfClubs = getCardValue ( "10♣" ) ;
69+ assertEquals ( tenOfClubs , 10 ) ;
70+
5171// Handle Ace (A):
5272// Given a card with a rank of "A",
5373// When the function is called with an Ace,
5474// Then it should, by default, assume the Ace is worth 11 points, which is a common rule in blackjack.
5575
76+ const aceCards = getCardValue ( "A♣" ) ;
77+ assertEquals ( aceCards , 11 ) ;
78+
5679// Handle Invalid Cards:
5780// Given a card with an invalid rank (neither a number nor a recognized face card),
5881// When the function is called with such a card,
5982// Then it should throw an error indicating "Invalid card rank."
83+
84+ try {
85+ getCardValue ( "Z♣" ) ; // invalid card
86+ console . log ( "Test failed: no error thrown" ) ;
87+ } catch ( error ) {
88+ assertEquals ( error . message , "Invalid Card" ) ;
89+ }
0 commit comments