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" ) {
1213 return 11 ;
1314 }
15+ if ( [ "10" , "J" , "Q" , "K" ] . includes ( rank ) ) {
16+ return 10 ;
17+ }
18+ if ( [ "2" , "3" , "4" , "5" , "6" , "7" , "8" , "9" ] . includes ( rank ) ) {
19+ return parseInt ( rank , 10 ) ;
20+ }
21+ return "Invalid card rank" ;
1422}
1523
1624// The line below allows us to load the getCardValue function into tests in other files.
@@ -38,20 +46,51 @@ assertEquals(aceofSpades, 11);
3846// Given a card with a rank between "2" and "9",
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).
41- const fiveofHearts = getCardValue ( "5♥" ) ;
49+ const fiveofHearts = getCardValue ( "5♥" ) ;
4250// ====> write your test here, and then add a line to pass the test in the function above
51+ assertEquals ( fiveofHearts , 5 ) ;
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 kingofDiamonds = getCardValue ( "K♦" ) ;
58+ assertEquals ( kingofDiamonds , 10 ) ;
59+
60+ const jackofHearts = getCardValue ( "J♥" ) ;
61+ assertEquals ( jackofHearts , 10 ) ;
62+
63+ const queenofSpades = getCardValue ( "Q♠" ) ;
64+ assertEquals ( queenofSpades , 10 ) ;
65+
66+ const tenofClubs = getCardValue ( "10♣" ) ;
67+ assertEquals ( tenofClubs , 10 ) ;
4868
4969// Handle Ace (A):
5070// Given a card with a rank of "A",
5171// When the function is called with an Ace,
5272// Then it should, by default, assume the Ace is worth 11 points, which is a common rule in blackjack.
73+ const aceofClubs = getCardValue ( "A♣" ) ;
74+ assertEquals ( aceofClubs , 11 ) ;
5375
5476// Handle Invalid Cards:
5577// Given a card with an invalid rank (neither a number nor a recognized face card),
5678// When the function is called with such a card,
5779// Then it should throw an error indicating "Invalid card rank."
80+ const invalidCard = getCardValue ( "1♠" ) ;
81+ assertEquals ( invalidCard , "Invalid card rank" ) ;
82+ const invalidCard2 = getCardValue ( "11♠" ) ;
83+ assertEquals ( invalidCard2 , "Invalid card rank" ) ;
84+ const invalidCard3 = getCardValue ( "B♠" ) ;
85+ assertEquals ( invalidCard3 , "Invalid card rank" ) ;
86+
87+
88+ console . log ( getCardValue ( "1♠" ) ) ;
89+ console . log ( getCardValue ( "11♠" ) ) ;
90+ console . log ( getCardValue ( "B♠" ) ) ;
91+ console . log ( getCardValue ( "3♠" ) ) ;
92+ console . log ( getCardValue ( "10♠" ) ) ;
93+ console . log ( getCardValue ( "J♠" ) ) ;
94+ console . log ( getCardValue ( "Q♠" ) ) ;
95+ console . log ( getCardValue ( "K♠" ) ) ;
96+ console . log ( getCardValue ( "A♠" ) ) ;
0 commit comments