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- if ( rank === "A" ) {
12- return 11 ;
13- }
11+ var rank = card . slice ( 0 , - 1 ) ; // get the rank of the card by removing the last character. (the suit is the last character)
12+ if ( rank === "A" ) return 11 ; // this checks for Aces
13+ if ( rank === "5" ) return 5 ; // this should check for the number 5
14+ if ( rank === "J" ) return 10 ; // this checks for Jacks
15+ if ( rank === "Q" ) return 10 ; // this checks for Queens
16+ if ( rank === "K" ) return 10 ; // this checks for Kings
17+ if ( rank === "10" ) return 10 ; // this checks for Tens
1418}
15-
16- // The line below allows us to load the getCardValue function into tests in other files.
17- // This will be useful in the "rewrite tests with jest" step.
18- module . exports = getCardValue ;
19-
2019// You need to write assertions for your function to check it works in different cases
2120// we're going to use this helper function to make our assertions easier to read
2221// if the actual output matches the target output, the test will pass
@@ -39,15 +38,20 @@ assertEquals(aceofSpades, 11);
3938// When the function is called with such a card,
4039// Then it should return the numeric value corresponding to the rank (e.g., "5" should return 5).
4140const fiveofHearts = getCardValue ( "5♥" ) ;
41+ assertEquals ( fiveofHearts , 5 ) ;
4242// ====> write your test here, and then add a line to pass the test in the function above
4343
4444// Handle Face Cards (J, Q, K):
4545// Given a card with a rank of "10," "J," "Q," or "K",
4646// When the function is called with such a card,
4747// Then it should return the value 10, as these cards are worth 10 points each in blackjack.
48+ const jackOfDiamonds = getCardValue ( "J♦" ) ;
49+ const queenOfClubs = getCardValue ( "Q♣" ) ;
50+ const kingOfSpades = getCardValue ( "K♠" ) ;
51+ assertEquals ( jackOfDiamonds , 10 ) ;
52+ assertEquals ( queenOfClubs , 10 ) ;
53+ assertEquals ( kingOfSpades , 10 ) ;
4854
49- // Handle Ace (A):
50- // Given a card with a rank of "A",
5155// When the function is called with an Ace,
5256// Then it should, by default, assume the Ace is worth 11 points, which is a common rule in blackjack.
5357
0 commit comments