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 . substring ( 0 , card . length - 1 ) ;
1112 if ( rank === "A" ) {
1213 return 11 ;
14+ } else if ( rank === "J" || rank === "Q" || rank === "K" || rank === "10" ) {
15+ return 10 ;
16+ } else if ( rank >= "2" && rank <= "9" ) {
17+ return parseInt ( rank ) ;
18+ } else {
19+ throw new Error ( "Invalid card rank." ) ;
1320 }
1421}
1522
@@ -39,19 +46,30 @@ assertEquals(aceofSpades, 11);
3946// When the function is called with such a card,
4047// Then it should return the numeric value corresponding to the rank (e.g., "5" should return 5).
4148const fiveofHearts = getCardValue ( "5♥" ) ;
49+ assertEquals ( fiveofHearts , 5 ) ;
4250// ====> write your test here, and then add a line to pass the test in the function above
4351
4452// Handle Face Cards (J, Q, K):
4553// Given a card with a rank of "10," "J," "Q," or "K",
4654// When the function is called with such a card,
4755// Then it should return the value 10, as these cards are worth 10 points each in blackjack.
4856
57+ const tenOfSpades = getCardValue ( "10♠" ) ;
58+ assertEquals ( tenOfSpades , 10 ) ;
59+
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.
5364
65+ const aceofHearts = getCardValue ( "A♠" ) ;
66+ assertEquals ( aceofSpades , 11 ) ;
67+
68+
5469// Handle Invalid Cards:
5570// Given a card with an invalid rank (neither a number nor a recognized face card),
5671// When the function is called with such a card,
5772// Then it should throw an error indicating "Invalid card rank."
73+
74+ const invalidCard = getCardValue ( "87" ) ;
75+ assertEquals ( invalidCard , "Invalid card rank" ) ;
0 commit comments