2222// execute the code to ensure all tests pass.
2323
2424function getCardValue ( card ) {
25- // TODO: Implement this function
26- }
25+
26+ if ( typeof card !== "string" || card . length < 2 ) {
27+ throw new Error ( "Invalid card" ) ;
28+ }
29+ // Handle "10" which is 2 characters
30+ if ( card . startsWith ( "10" ) ) {
31+ return 10 ;
32+ }
33+
34+ const firstChar = card [ 0 ] ;
35+
36+ // check if picture cards
37+ if ( firstChar === "A" ) return 11 ;
38+ if ( firstChar === "J" || firstChar === "Q" || firstChar === "K" ) return 10 ;
39+
40+ // check if number is between 2 and 9, 10 has already been checked for and there should be no other valid cards
41+
42+ const num = Number ( firstChar ) ;
2743
44+ if ( ! isNaN ( num ) && num >= 2 && num <= 9 ) {
45+ return num ;
46+ // for everything else
47+ } else {
48+ throw new Error ( "Invalid card" ) ;
49+ }
50+ }
2851// The line below allows us to load the getCardValue function into tests in other files.
2952// This will be useful in the "rewrite tests with jest" step.
3053module . exports = getCardValue ;
@@ -40,13 +63,59 @@ function assertEquals(actualOutput, targetOutput) {
4063// TODO: Write tests to cover all outcomes, including throwing errors for invalid cards.
4164// Examples:
4265assertEquals ( getCardValue ( "9♠" ) , 9 ) ;
66+ assertEquals ( getCardValue ( "10♥" ) , 10 ) ;
67+ assertEquals ( getCardValue ( "J♥" ) , 10 ) ;
68+ assertEquals ( getCardValue ( "A♠" ) , 11 ) ;
69+ assertEquals ( getCardValue ( "Q♦" ) , 10 ) ;
70+ assertEquals ( getCardValue ( "K♣" ) , 10 ) ;
4371
4472// Handling invalid cards
73+ try {
74+ getCardValue ( "♠J" ) ;
75+ console . error ( "Error was not thrown for invalid card" ) ;
76+ } catch ( e ) { }
77+
4578try {
4679 getCardValue ( "invalid" ) ;
80+ console . error ( "Error was not thrown for invalid card" ) ;
81+ } catch ( e ) { }
4782
48- // This line will not be reached if an error is thrown as expected
83+ // Handling invalid cards
84+ try {
85+ getCardValue ( "♠J" ) ;
4986 console . error ( "Error was not thrown for invalid card" ) ;
5087} catch ( e ) { }
5188
89+ try {
90+ getCardValue ( "invalid" ) ;
91+ console . error ( "Error was not thrown for invalid card" ) ;
92+ } catch ( e ) { }
93+
94+
95+ try {
96+ getCardValue ( "♠J" ) ;
97+ console . error ( "Error was not thrown for invalid card" ) ;
98+ } catch ( e ) {
99+ console . log ( "Invalid card detected" ) ;
100+ }
101+
102+ try {
103+ getCardValue ( "invalid" ) ;
104+ console . error ( "Error was not thrown for invalid card" ) ;
105+ } catch ( e ) { }
106+
107+ console . log ( getCardValue ( "9♠" ) ) ;
108+ console . log ( getCardValue ( "10♥" ) ) ;
109+ console . log ( getCardValue ( "J♥" ) ) ;
110+ console . log ( getCardValue ( "A♠" ) ) ;
111+ console . log ( getCardValue ( "Q♦" ) ) ;
112+ console . log ( getCardValue ( "K♣" ) ) ;
113+
114+ // This line will not be reached if an error is thrown as expected
115+ try {
116+ sole . error ( "Error was not thrown for invalid card" ) ;
117+ } catch ( e ) { }
118+
52119// What other invalid card cases can you think of?
120+ // There could be cards with special characters
121+ // There could be cards with two numbers rather than a number and a suite
0 commit comments