2222// execute the code to ensure all tests pass.
2323
2424function getCardValue ( card ) {
25- // TODO: Implement this function
25+ const ranks = [ "A" , "2" , "3" , "4" , "5" , "6" , "7" , "8" , "9" , "10" , "J" , "Q" , "K" ] ;
26+ const suits = [ "♠" , "♥" , "♦" , "♣" ] ;
27+ const rankPart = card . slice ( 0 , card . length - 1 ) ;
28+ const suitPart = card . slice ( - 1 ) ;
29+
30+ if ( ranks . includes ( rankPart ) && suits . includes ( suitPart ) ) {
31+ if ( rankPart == "A" ) {
32+ return 11 ;
33+ } else if ( rankPart == "J" || rankPart == "Q" || rankPart == "K" ) {
34+ return 10 ;
35+ } else {
36+ return Number ( rankPart ) ;
37+ }
38+ } else {
39+ throw new Error ( "Error" ) ;
40+ }
2641}
2742
2843// The line below allows us to load the getCardValue function into tests in other files.
@@ -41,6 +56,12 @@ function assertEquals(actualOutput, targetOutput) {
4156// Examples:
4257assertEquals ( getCardValue ( "9♠" ) , 9 ) ;
4358
59+ assertEquals ( getCardValue ( "10♣" ) , 10 ) ;
60+ assertEquals ( getCardValue ( "J♥" ) , 10 ) ;
61+ assertEquals ( getCardValue ( "K♥" ) , 10 ) ;
62+ assertEquals ( getCardValue ( "A♦" ) , 11 ) ;
63+ assertEquals ( getCardValue ( "2♦" ) , 2 ) ;
64+
4465// Handling invalid cards
4566try {
4667 getCardValue ( "invalid" ) ;
5071} catch ( e ) { }
5172
5273// What other invalid card cases can you think of?
74+ assertEquals ( getCardValue ( "♦♦" ) , "invalid" ) ; // throws an error for this example
75+ assertEquals ( getCardValue ( "12" ) , "Error" ) ;
0 commit comments