2222// execute the code to ensure all tests pass.
2323
2424function getCardValue ( card ) {
25- // TODO: Implement this function
25+ if (
26+ ! card . includes ( "♠" ) &&
27+ ! card . includes ( "♥" ) &&
28+ ! card . includes ( "♦" ) &&
29+ ! card . includes ( "♣" )
30+ ) {
31+ return "Invalid String" ;
32+ }
33+ const rank = card . slice ( 0 , - 1 ) ;
34+
35+ if (
36+ ! card . includes ( "A" ) &&
37+ ! card . includes ( "J" ) &&
38+ ! card . includes ( "Q" ) &&
39+ ! card . includes ( "K" ) &&
40+ ! ( rank >= 2 && rank <= 10 )
41+ ) {
42+ return "Invalid String" ;
43+ }
44+
45+ if ( card . includes ( "A" ) ) {
46+ return 11 ;
47+ } else if ( card . includes ( "J" ) || card . includes ( "Q" ) || card . includes ( "K" ) ) {
48+ return 10 ;
49+ } else if ( rank >= 2 && rank <= 10 ) {
50+ return rank ;
51+ }
2652}
2753
2854// The line below allows us to load the getCardValue function into tests in other files.
@@ -40,6 +66,17 @@ function assertEquals(actualOutput, targetOutput) {
4066// TODO: Write tests to cover all outcomes, including throwing errors for invalid cards.
4167// Examples:
4268assertEquals ( getCardValue ( "9♠" ) , 9 ) ;
69+ assertEquals ( getCardValue ( "A♠" ) , 11 ) ;
70+ assertEquals ( getCardValue ( "J♠" ) , 10 ) ;
71+ assertEquals ( getCardValue ( "K♠" ) , 10 ) ;
72+ assertEquals ( getCardValue ( "Q♠" ) , 10 ) ;
73+ assertEquals ( getCardValue ( "2♠" ) , 2 ) ;
74+ assertEquals ( getCardValue ( "10♠" ) , 10 ) ;
75+ assertEquals ( getCardValue ( " ♠" ) , "Invalid String" ) ;
76+ assertEquals ( getCardValue ( " 6 ♠" ) , "Invalid String" ) ;
77+ assertEquals ( getCardValue ( " ♠" ) , "Invalid String" ) ;
78+ assertEquals ( getCardValue ( " **♠" ) , "Invalid String" ) ;
79+ assertEquals ( getCardValue ( "10 **" ) , "Invalid String" ) ;
4380
4481// Handling invalid cards
4582try {
5087} catch ( e ) { }
5188
5289// What other invalid card cases can you think of?
90+ // card with a mix of suit
91+ //card with 0 at the front 01♠
0 commit comments