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 suit
12+
1113 if ( rank === "A" ) {
1214 return 11 ;
15+ } else if ( [ "K" , "Q" , "J" , "10" ] . includes ( rank ) ) {
16+ return 10 ;
17+ } else if ( [ "2" , "3" , "4" , "5" , "6" , "7" , "8" , "9" ] . includes ( rank ) ) {
18+ return Number ( rank ) ;
19+ } else {
20+ throw new Error ( "Invalid card rank." ) ;
1321 }
1422}
1523
@@ -39,12 +47,25 @@ assertEquals(aceofSpades, 11);
3947// When the function is called with such a card,
4048// Then it should return the numeric value corresponding to the rank (e.g., "5" should return 5).
4149const fiveofHearts = getCardValue ( "5♥" ) ;
50+ assertEquals ( fiveofHearts , 5 ) ;
4251// ====> write your test here, and then add a line to pass the test in the function above
4352
4453// Handle Face Cards (J, Q, K):
4554// Given a card with a rank of "10," "J," "Q," or "K",
4655// When the function is called with such a card,
4756// Then it should return the value 10, as these cards are worth 10 points each in blackjack.
57+ const tenOfDiamonds = getCardValue ( "10♦" ) ;
58+ assertEquals ( tenOfDiamonds , 10 ) ;
59+
60+ const jackOfClubs = getCardValue ( "J♣" ) ;
61+ assertEquals ( jackOfClubs , 10 ) ;
62+
63+ const queenOfSpades = getCardValue ( "Q♠" ) ;
64+ assertEquals ( queenOfSpades , 10 ) ;
65+
66+ const kingOfHearts = getCardValue ( "K♥" ) ;
67+ assertEquals ( kingOfHearts , 10 ) ;
68+
4869
4970// Handle Ace (A):
5071// Given a card with a rank of "A",
@@ -55,3 +76,10 @@ const fiveofHearts = getCardValue("5♥");
5576// Given a card with an invalid rank (neither a number nor a recognized face card),
5677// When the function is called with such a card,
5778// Then it should throw an error indicating "Invalid card rank."
79+
80+ try {
81+ getCardValue ( "1♠" ) ;
82+ console . error ( "Test failed: invalid card did not throw error" ) ;
83+ } catch ( error ) {
84+ console . log ( "Invalid card test passed" ) ;
85+ }
0 commit comments