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+ let rank = card . slice ( 0 , - 1 ) ;
1112 if ( rank === "A" ) {
1213 return 11 ;
1314 }
15+ if ( rank === "J" || rank === "Q" || rank === "K" ) {
16+ return 10 ;
17+ }
18+ if ( ( rank => 2 ) && ( rank <= 10 ) ) {
19+ return Number ( rank ) ;
20+ }
21+ return "Invalid card rank." ;
1422}
1523
1624// The line below allows us to load the getCardValue function into tests in other files.
@@ -40,18 +48,25 @@ assertEquals(aceofSpades, 11);
4048// Then it should return the numeric value corresponding to the rank (e.g., "5" should return 5).
4149const 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 faceCards = getCardValue ( "J♠" ) ;
58+ assertEquals ( faceCards , 10 ) ;
4859
4960// Handle Ace (A):
5061// Given a card with a rank of "A",
5162// When the function is called with an Ace,
5263// Then it should, by default, assume the Ace is worth 11 points, which is a common rule in blackjack.
64+ const ace = getCardValue ( "A♠" ) ;
65+ assertEquals ( ace , 11 ) ;
5366
5467// Handle Invalid Cards:
5568// Given a card with an invalid rank (neither a number nor a recognized face card),
5669// When the function is called with such a card,
5770// Then it should throw an error indicating "Invalid card rank."
71+ const invalidCards = getCardValue ( "hello" ) ;
72+ assertEquals ( invalidCards , "Invalid card rank." ) ;
0 commit comments