1010function getCardValue ( card ) {
1111 var rank = card . slice ( 0 , - 1 ) ; // get the rank of the card by removing the last character. (the suit is the last character)
1212 if ( rank === "A" ) return 11 ; // this checks for Aces
13+ // Handle Number Cards (2-9)
1314 if ( rank === "2" ) return 2 ; // this checks for the twos
1415 if ( rank === "3" ) return 3 ; // this checks for the threes
1516 if ( rank === "4" ) return 4 ; // this checks for the fours
@@ -18,10 +19,13 @@ function getCardValue(card) {
1819 if ( rank === "7" ) return 7 ; // this checks for the sevens
1920 if ( rank === "8" ) return 8 ; // this checks for the eights
2021 if ( rank === "9" ) return 9 ; // this checks for the nines
22+ // Handle Face Cards (J, Q, K) And 10's
2123 if ( rank === "J" ) return 10 ; // this checks for Jacks
2224 if ( rank === "Q" ) return 10 ; // this checks for Queens
2325 if ( rank === "K" ) return 10 ; // this checks for Kings
2426 if ( rank === "10" ) return 10 ; // this checks for Tens
27+ // if none of the above its an invalid card and throw an error
28+ throw new Error ( "Invalid card rank." ) ; // this will throw an error if the card is not a valid rank
2529}
2630// You need to write assertions for your function to check it works in different cases
2731// we're going to use this helper function to make our assertions easier to read
@@ -44,6 +48,7 @@ assertEquals(aceofSpades, 11);
4448// Given a card with a rank between "2" and "9",
4549// When the function is called with such a card,
4650// Then it should return the numeric value corresponding to the rank (e.g., "5" should return 5).
51+ // ====> write your test here, and then add a line to pass the test in the function above
4752const fiveofHearts = getCardValue ( "5♥" ) ;
4853const sixofDiamonds = getCardValue ( "6♦" ) ;
4954const sevenofClubs = getCardValue ( "7♣" ) ;
@@ -52,7 +57,6 @@ assertEquals(fiveofHearts, 5);
5257assertEquals ( sixofDiamonds , 6 ) ;
5358assertEquals ( sevenofClubs , 7 ) ;
5459assertEquals ( eightofSpades , 8 ) ;
55- // ====> write your test here, and then add a line to pass the test in the function above
5660
5761// Handle Face Cards (J, Q, K):
5862// Given a card with a rank of "10," "J," "Q," or "K",
@@ -72,4 +76,9 @@ assertEquals(kingOfSpades, 10);
7276// Given a card with an invalid rank (neither a number nor a recognized face card),
7377// When the function is called with such a card,
7478// Then it should throw an error indicating "Invalid card rank."
75-
79+ try {
80+ getCardValue ( "z♠" ) ; // this should throw an error of "Invalid card rank."
81+ console . log ( "Test failed: Expected an error for invalid card rank." ) ;
82+ } catch ( error ) {
83+ assertEquals ( error . meessage , "Invalid card rank." ) ;
84+ }
0 commit comments