2222// execute the code to ensure all tests pass.
2323
2424function getCardValue ( card ) {
25-
2625 if ( typeof card !== "string" || card . length < 2 ) {
27- throw new Error ( "Invalid card" ) ;
26+ throw new Error ( "Invalid card" ) ;
2827 }
29- // Handle "10" which is 2 characters
28+ // Handle "10" which is like "10♥"
3029 if ( card . startsWith ( "10" ) ) {
3130 return 10 ;
3231 }
32+ //if more than 2 characters and not starting with a 10 card
33+ if ( card . length > 2 ) {
34+ throw new Error ( "Invalid card" ) ;
35+ }
36+
37+ // remaining cards must be 2 characters long
38+
39+ const validSuits = [ "♠" , "♥" , "♦" , "♣" ] ;
40+ const suit = card [ card . length - 1 ] ;
3341
3442 const firstChar = card [ 0 ] ;
3543
3644 // check if picture cards
3745 if ( firstChar === "A" ) return 11 ;
38- if ( firstChar === "J" || firstChar === "Q" || firstChar === "K" ) return 10 ;
46+ if ( firstChar === "J" || firstChar === "Q" || firstChar === "K" ) return 10 ;
3947
4048 // check if number is between 2 and 9, 10 has already been checked for and there should be no other valid cards
4149
4250 const num = Number ( firstChar ) ;
4351
4452 if ( ! isNaN ( num ) && num >= 2 && num <= 9 ) {
4553 return num ;
46- // for everything else
47- } else {
48- throw new Error ( "Invalid card" ) ;
49- }
54+ // for everything else
55+ } else {
56+ throw new Error ( "Invalid card" ) ;
57+ }
5058}
5159// The line below allows us to load the getCardValue function into tests in other files.
5260// This will be useful in the "rewrite tests with jest" step.
@@ -69,17 +77,6 @@ assertEquals(getCardValue("A♠"), 11);
6977assertEquals ( getCardValue ( "Q♦" ) , 10 ) ;
7078assertEquals ( getCardValue ( "K♣" ) , 10 ) ;
7179
72- // Handling invalid cards
73- try {
74- getCardValue ( "♠J" ) ;
75- console . error ( "Error was not thrown for invalid card" ) ;
76- } catch ( e ) { }
77-
78- try {
79- getCardValue ( "invalid" ) ;
80- console . error ( "Error was not thrown for invalid card" ) ;
81- } catch ( e ) { }
82-
8380// Handling invalid cards
8481try {
8582 getCardValue ( "♠J" ) ;
9188 console . error ( "Error was not thrown for invalid card" ) ;
9289} catch ( e ) { }
9390
94-
9591try {
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" ) ;
92+ getCardValue ( "22" ) ;
10493 console . error ( "Error was not thrown for invalid card" ) ;
10594} catch ( e ) { }
10695
@@ -110,12 +99,24 @@ console.log(getCardValue("J♥"));
11099console . log ( getCardValue ( "A♠" ) ) ;
111100console . log ( getCardValue ( "Q♦" ) ) ;
112101console . log ( getCardValue ( "K♣" ) ) ;
113-
114- // This line will not be reached if an error is thrown as expected
102+
103+ // This line will not be reached if an error is thrown as expected
115104try {
116- sole . error ( "Error was not thrown for invalid card" ) ;
105+ console . error ( "Error was not thrown for invalid card" ) ;
117106} catch ( e ) { }
118107
119108// What other invalid card cases can you think of?
120- // There could be cards with special characters
109+
110+ // There could be cards with special characters.
111+
121112// There could be cards with two numbers rather than a number and a suite
113+ // These will not be picked up because the code only checks for if starts with 10 or if the first character
114+ // is a number between 2 and 9, so cards like "22" would be valid because the first number
115+ // is 2, but the second character is not checked for validity. It is also 2 characters
116+ // so will not cause an error when the length is checked.
117+
118+ // Since the second character is not checked it could be 2D which is not a valid card but
119+ // would be accepted because the first character is 2 and the second character is not checked for validity
120+
121+ // When the card is checked if it begins with 10 it does check if it has a valid suite
122+ // as only the first 2 characters are checked so it could be 10DEVON or 10♥♥.
0 commit comments