2222// execute the code to ensure all tests pass.
2323
2424function getCardValue ( card ) {
25- // TODO: Implement this function
25+ const rank = card . slice ( 0 , - 1 ) ;
26+
27+ if ( rank === "A" ) return 11 ;
28+ if ( [ "K" , "Q" , "J" , "10" ] . includes ( rank ) ) return 10 ;
29+ if ( [ "2" , "3" , "4" , "5" , "6" , "7" , "8" , "9" ] . includes ( rank ) )
30+ return Number ( rank ) ;
31+
32+ throw new Error ( "invalid card rank." ) ;
2633}
34+ // TODO: Implement this function
2735
2836// The line below allows us to load the getCardValue function into tests in other files.
2937// This will be useful in the "rewrite tests with jest" step.
@@ -40,6 +48,11 @@ function assertEquals(actualOutput, targetOutput) {
4048// TODO: Write tests to cover all outcomes, including throwing errors for invalid cards.
4149// Examples:
4250assertEquals ( getCardValue ( "9♠" ) , 9 ) ;
51+ assertEquals ( getCardValue ( "A♣" ) , 11 ) ;
52+ assertEquals ( getCardValue ( "J♥" ) , 10 ) ;
53+ assertEquals ( getCardValue ( "Q♦" ) , 10 ) ;
54+ assertEquals ( getCardValue ( "K♠" ) , 10 ) ;
55+ assertEquals ( getCardValue ( "10♣" ) , 10 ) ;
4356
4457// Handling invalid cards
4558try {
5063} catch ( e ) { }
5164
5265// What other invalid card cases can you think of?
66+ assertEquals ( getCardValue ( "2♦" ) , 2 ) ;
67+ assertEquals ( getCardValue ( "5♥" ) , 5 ) ;
0 commit comments