2222// execute the code to ensure all tests pass.
2323
2424function getCardValue ( card ) {
25- if ( typeof card !== "string" || card . length < 2 ) {
26- throw new Error ( "Invalid card" ) ;
27- }
28- // Handle "10" which is like "10♥"
29- if ( card . startsWith ( "10" ) ) {
30- return 10 ;
31- }
32- //if more than 2 characters and not starting with a 10 card
33- if ( card . length > 2 ) {
25+ // if card is less than 2 characters or more than 3 characters it is invalid
26+ // and check if not a string
27+
28+ if ( typeof card !== "string" || card . length < 2 || card . length > 3 ) {
3429 throw new Error ( "Invalid card" ) ;
3530 }
3631
37- // remaining cards must be 2 characters long
38-
3932 const validSuits = [ "♠" , "♥" , "♦" , "♣" ] ;
40- const suit = card [ card . length - 1 ] ;
4133
42- const firstChar = card [ 0 ] ;
34+ let firstChar , suit ;
4335
44- // check if picture cards
45- if ( firstChar === "A" ) return 11 ;
46- if ( firstChar === "J" || firstChar === "Q" || firstChar === "K" ) return 10 ;
36+ if ( card . startsWith ( "10" ) ) {
37+ firstChar = "10" ;
38+ suit = card [ 2 ] ; //if it starts with 10 the third character is the suit
39+ } else {
40+ firstChar = card [ 0 ] ; //otherwise
41+ suit = card [ 1 ] ; //the second character is the suit
42+ }
43+ if ( ! suit || ! validSuits . includes ( suit ) ) {
44+ throw new Error ( "Invalid suit" ) ;
45+ }
4746
48- // check if number is between 2 and 9, 10 has already been checked for and there should be no other valid cards
47+ // check if picture cards
48+ if ( firstChar === "A" ) return 11 ; // if Ace return 11
49+ if ( [ "J" , "Q" , "K" ] . includes ( firstChar ) ) return 10 ; // if Jack, Queen or King return 10
4950
5051 const num = Number ( firstChar ) ;
52+ if ( num >= 2 && num <= 10 ) return num ; //checks number is between 2 and 10
5153
52- if ( ! isNaN ( num ) && num >= 2 && num <= 9 ) {
53- return num ;
54- // for everything else
55- } else {
56- throw new Error ( "Invalid card" ) ;
57- }
54+ // for everything else that is invalid
55+ throw new Error ( "Invalid number" ) ;
5856}
57+
5958// The line below allows us to load the getCardValue function into tests in other files.
6059// This will be useful in the "rewrite tests with jest" step.
6160module . exports = getCardValue ;
@@ -80,17 +79,19 @@ assertEquals(getCardValue("K♣"), 10);
8079// Handling invalid cards
8180try {
8281 getCardValue ( "♠J" ) ;
83- console . error ( "Error was not thrown for invalid card " ) ;
82+ console . error ( "Error was not thrown for invalid suit first " ) ;
8483} catch ( e ) { }
8584
8685try {
8786 getCardValue ( "invalid" ) ;
88- console . error ( "Error was not thrown for invalid card" ) ;
89- } catch ( e ) { }
87+ console . error ( "Test failed: invalid card accepted" ) ;
88+ } catch ( e ) {
89+ console . log ( "Test passed: Invalid card rejected" ) ;
90+ }
9091
9192try {
9293 getCardValue ( "22" ) ;
93- console . error ( "Error was not thrown for invalid card " ) ;
94+ console . error ( "Error was not thrown for invalid number " ) ;
9495} catch ( e ) { }
9596
9697console . log ( getCardValue ( "9♠" ) ) ;
@@ -100,16 +101,11 @@ console.log(getCardValue("A♠"));
100101console . log ( getCardValue ( "Q♦" ) ) ;
101102console . log ( getCardValue ( "K♣" ) ) ;
102103
103- // This line will not be reached if an error is thrown as expected
104- try {
105- console . error ( "Error was not thrown for invalid card" ) ;
106- } catch ( e ) { }
107-
108104// What other invalid card cases can you think of?
109105
110106// There could be cards with special characters.
111107
112- // There could be cards with two numbers rather than a number and a suite
108+ // There could be cards with two numbers rather than a number and a suit
113109// These will not be picked up because the code only checks for if starts with 10 or if the first character
114110// is a number between 2 and 9, so cards like "22" would be valid because the first number
115111// is 2, but the second character is not checked for validity. It is also 2 characters
@@ -118,5 +114,5 @@ try {
118114// Since the second character is not checked it could be 2D which is not a valid card but
119115// would be accepted because the first character is 2 and the second character is not checked for validity
120116
121- // When the card is checked if it begins with 10 it does check if it has a valid suite
117+ // When the card is checked if it begins with 10 it does not check if it has a valid suit
122118// as only the first 2 characters are checked so it could be 10DEVON or 10♥♥.
0 commit comments