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- 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- // Handle Number Cards (2-9)
14- if ( rank === "2" ) return 2 ; // this checks for the twos
15- if ( rank === "3" ) return 3 ; // this checks for the threes
16- if ( rank === "4" ) return 4 ; // this checks for the fours
17- if ( rank === "5" ) return 5 ; // this should check for fives
18- if ( rank === "6" ) return 6 ; // this checks for the sixes
19- if ( rank === "7" ) return 7 ; // this checks for the sevens
20- if ( rank === "8" ) return 8 ; // this checks for the eights
21- if ( rank === "9" ) return 9 ; // this checks for the nines
22- // Handle Face Cards (J, Q, K) And 10's
23- if ( rank === "J" ) return 10 ; // this checks for Jacks
24- if ( rank === "Q" ) return 10 ; // this checks for Queens
25- if ( rank === "K" ) return 10 ; // this checks for Kings
26- 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
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+ // Handle Number Cards (2-9)
14+ if ( rank === "2" ) return 2 ; // this checks for the twos
15+ if ( rank === "3" ) return 3 ; // this checks for the threes
16+ if ( rank === "4" ) return 4 ; // this checks for the fours
17+ if ( rank === "5" ) return 5 ; // this should check for fives
18+ if ( rank === "6" ) return 6 ; // this checks for the sixes
19+ if ( rank === "7" ) return 7 ; // this checks for the sevens
20+ if ( rank === "8" ) return 8 ; // this checks for the eights
21+ if ( rank === "9" ) return 9 ; // this checks for the nines
22+ // Handle Face Cards (J, Q, K) And 10's
23+ if ( rank === "J" ) return 10 ; // this checks for Jacks
24+ if ( rank === "Q" ) return 10 ; // this checks for Queens
25+ if ( rank === "K" ) return 10 ; // this checks for Kings
26+ 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
2929}
3030// You need to write assertions for your function to check it works in different cases
3131// we're going to use this helper function to make our assertions easier to read
@@ -64,7 +64,7 @@ assertEquals(eightofSpades, 8);
6464// Then it should return the value 10, as these cards are worth 10 points each in blackjack.
6565const jackOfDiamonds = getCardValue ( "J♦" ) ;
6666const queenOfClubs = getCardValue ( "Q♣" ) ;
67- const kingOfSpades = getCardValue ( "K♠" ) ;
67+ const kingOfSpades = getCardValue ( "K♠" ) ;
6868assertEquals ( jackOfDiamonds , 10 ) ;
6969assertEquals ( queenOfClubs , 10 ) ;
7070assertEquals ( kingOfSpades , 10 ) ;
@@ -79,7 +79,7 @@ assertEquals(kingOfSpades, 10);
7979try {
8080 getCardValue ( "z♠" ) ; // this should throw an error of "Invalid card rank."
8181 console . log ( "Test failed: Expected an error for invalid card rank." ) ;
82- } catch ( error ) {
82+ } catch ( error ) {
8383 assertEquals ( error . message , "Invalid card rank." ) ;
8484}
85- module . exports = getCardValue ;
85+ module . exports = getCardValue ;
0 commit comments