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 ( rank === "J" || rank === "Q" || rank === "K" )
16+ {
17+ return 10 ;
18+ }
19+ const convertTheStringtoNumber = Number ( rank ) ;
20+ if ( convertTheStringtoNumber >= 2 && convertTheStringtoNumber <= 10 ) {
21+ return convertTheStringtoNumber ;
22+ }
23+ else
24+ {
25+ return "Invalid card rank" ;
26+ }
1427}
15-
1628// The line below allows us to load the getCardValue function into tests in other files.
1729// This will be useful in the "rewrite tests with jest" step.
1830module . exports = getCardValue ;
@@ -40,18 +52,26 @@ assertEquals(aceofSpades, 11);
4052// Then it should return the numeric value corresponding to the rank (e.g., "5" should return 5).
4153const fiveofHearts = getCardValue ( "5♥" ) ;
4254// ====> write your test here, and then add a line to pass the test in the function above
43-
55+ assertEquals ( fiveofHearts , 5 ) ;
4456// Handle Face Cards (J, Q, K):
4557// Given a card with a rank of "10," "J," "Q," or "K",
4658// When the function is called with such a card,
4759// Then it should return the value 10, as these cards are worth 10 points each in blackjack.
48-
60+ const jackofHearts = getCardValue ( "J♥" ) ;
61+ assertEquals ( jackofHearts , 10 ) ;
62+ const queenofSpades = getCardValue ( "Q♠" ) ;
63+ assertEquals ( queenofSpades , 10 ) ;
64+ const kingofSpades = getCardValue ( "K♠" ) ;
65+ assertEquals ( kingofSpades , 10 ) ;
4966// Handle Ace (A):
5067// Given a card with a rank of "A",
5168// When the function is called with an Ace,
5269// Then it should, by default, assume the Ace is worth 11 points, which is a common rule in blackjack.
53-
70+ const aceofHearts = getCardValue ( "A♥" ) ;
71+ assertEquals ( aceofHearts , 11 ) ;
5472// Handle Invalid Cards:
5573// Given a card with an invalid rank (neither a number nor a recognized face card),
5674// When the function is called with such a card,
5775// Then it should throw an error indicating "Invalid card rank."
76+ const invalidCards = getCardValue ( "B♥" ) ;
77+ assertEquals ( invalidCards , "Invalid card rank" ) ;
0 commit comments