2323
2424// TODO: Implement this function
2525function getCardValue ( card ) {
26- const validRanks = [ "A" , "2" , "3" , "4" , "5" , "6" , "7" , "8" , "9" , "10" , "J" , "Q" , "K" ] ;
27- const validSuits = [ "♠" , "♥" , "♦" , "♣" ] ;
28- //Validation
29- // Must be a string
30- if ( typeof card !== "string" ) {
31- throw Error ( "Invalid card" ) ;
26+ // 1. declare variables in an array to use for validation
27+ const validRanks = [
28+ "A" ,
29+ "2" ,
30+ "3" ,
31+ "4" ,
32+ "5" ,
33+ "6" ,
34+ "7" ,
35+ "8" ,
36+ "9" ,
37+ "10" ,
38+ "J" ,
39+ "Q" ,
40+ "K" ,
41+ ] ;
42+ const validSuits = [ "♠" , "♥" , "♦" , "♣" ] ;
43+ //2. Check that input is a string
44+ if ( typeof card !== "string" ) {
45+ throw new Error ( "Invalid card" ) ;
3246 }
33- // Must be at least 2 characters and maximum of 3
34- //
35- //
36- }
37- // ---------- INVALID CHECKS FIRST ----------
38-
39- // 1. Must be a string
40-
41-
42- // 2. Must have at least 2 characters (rank + suit)
43- if ( card . length < 2 ) {
47+ // 3. Must have at least 2 characters but 3 or less (rank and suit)
48+ if ( card . length < 2 || card . length > 3 ) {
4449 throw new Error ( "Invalid card" ) ;
4550 }
4651
47- // 3. Extract suit and rank
52+ // 4. Defining the suit and rank
53+ //Select character index -1, the last character only.
4854 const suit = card . slice ( - 1 ) ;
55+ //select everything from index 0 and stop before the last character
4956 const rank = card . slice ( 0 , - 1 ) ;
5057
51- // 4. Suit must be valid
58+ // 5. Checking suit validity
59+ //Checks if suit is in the array
5260 if ( ! validSuits . includes ( suit ) ) {
5361 throw new Error ( "Invalid card" ) ;
5462 }
55-
56- // 5. Rank must be valid
63+ // 6. Checking rank validity
64+ //Checks if rank is in the array
5765 if ( ! validRanks . includes ( rank ) ) {
5866 throw new Error ( "Invalid card" ) ;
5967 }
60-
61- // ---------- ALL INPUT IS NOW VALID ----------
62-
68+ //7. Determines the output values for special cards
6369 if ( rank === "A" ) return 11 ;
6470 if ( [ "J" , "Q" , "K" ] . includes ( rank ) ) return 10 ;
65-
71+ // 8. Otherwise it returns the number of the card
6672 return Number ( rank ) ;
6773}
74+
6875// The line below allows us to load the getCardValue function into tests in other files.
6976// This will be useful in the "rewrite tests with jest" step.
7077module . exports = getCardValue ;
@@ -80,13 +87,43 @@ function assertEquals(actualOutput, targetOutput) {
8087// TODO: Write tests to cover all outcomes, including throwing errors for invalid cards.
8188// Examples:
8289assertEquals ( getCardValue ( "9♠" ) , 9 ) ;
90+ assertEquals ( getCardValue ( "A♠" ) , 11 ) ;
91+ assertEquals ( getCardValue ( "Q♥" ) , 10 ) ;
92+ assertEquals ( getCardValue ( "K♦" ) , 10 ) ;
93+ assertEquals ( getCardValue ( "J♣" ) , 10 ) ;
8394
8495// Handling invalid cards
8596try {
8697 getCardValue ( "invalid" ) ;
8798
8899 // This line will not be reached if an error is thrown as expected
89100 console . error ( "Error was not thrown for invalid card" ) ;
90- } catch ( e ) { }
101+ } catch ( error ) { }
91102
92103// What other invalid card cases can you think of?
104+ try {
105+ getCardValue ( "789" ) ;
106+
107+ // This line will not be reached if an error is thrown as expected
108+ console . error ( "Error was not thrown for invalid card" ) ;
109+ } catch ( error ) { }
110+
111+ try {
112+ getCardValue ( "" ) ;
113+ console . error ( "Error was not thrown for invalid card" ) ;
114+ } catch ( e ) { }
115+
116+ try {
117+ getCardValue ( "♠♥♦♣" ) ;
118+ console . error ( "Error was not thrown for invalid card" ) ;
119+ } catch ( error ) { }
120+
121+ try {
122+ getCardValue ( "5$" ) ;
123+ console . error ( "Error was not thrown for invalid card" ) ;
124+ } catch ( error ) { }
125+
126+ try {
127+ getCardValue ( "£50" ) ;
128+ console . error ( "Error was not thrown for invalid card" ) ;
129+ } catch ( error ) { }
0 commit comments