2222// execute the code to ensure all tests pass.
2323
2424function getCardValue ( card ) {
25- // TODO: Implement this function
25+ const suit = [ "♠" , "♥" , "♦" , "♣" ] ; // array with suits
26+ const rank = [
27+ "2" ,
28+ "3" ,
29+ "4" ,
30+ "5" ,
31+ "6" ,
32+ "7" ,
33+ "8" ,
34+ "9" ,
35+ "10" ,
36+ "A" ,
37+ "K" ,
38+ "Q" ,
39+ "J" ,
40+ ] ; // array with ranks
41+ const tenPointsFaceCards = [ "K" , "Q" , "J" ] ; //cards with value of 10 points
42+
43+ const rank1 = card . slice ( 0 , - 1 ) ; //takes the firs part of card
44+ const suit1 = card . slice ( - 1 ) ; //takes "suit" secnd part of card
45+ const isCardValid = suit . includes ( suit1 ) && rank . includes ( rank1 ) ; //conditions where card is considered valid
46+
47+ //conditional statement to check validity of the card
48+ if ( ! isCardValid ) {
49+ throw new Error ( "Invalid card format" ) ;
50+ }
51+
52+ //conditional statements to check what value to return depending on the card value
53+ if ( rank1 === "A" ) {
54+ return 11 ;
55+ } else if ( tenPointsFaceCards . includes ( rank1 ) ) {
56+ return 10 ;
57+ } else {
58+ return Number ( rank1 ) ;
59+ }
2660}
2761
2862// The line below allows us to load the getCardValue function into tests in other files.
@@ -39,8 +73,23 @@ function assertEquals(actualOutput, targetOutput) {
3973
4074// TODO: Write tests to cover all outcomes, including throwing errors for invalid cards.
4175// Examples:
76+
4277assertEquals ( getCardValue ( "9♠" ) , 9 ) ;
4378
79+ assertEquals ( getCardValue ( "A♠" ) , 11 ) ;
80+
81+ assertEquals ( getCardValue ( "K♠" ) , 10 ) ;
82+
83+ assertEquals ( getCardValue ( "Q♠" ) , 10 ) ;
84+
85+ assertEquals ( getCardValue ( "J♠" ) , 10 ) ;
86+
87+ assertEquals ( getCardValue ( "10♣" ) , 10 ) ;
88+
89+ //assertEquals(getCardValue("11♣"), "Invalid card format"); i find that
90+ // this test will not work, because my function is throwing error before the program
91+ // reaches the assertEquals function.
92+
4493// Handling invalid cards
4594try {
4695 getCardValue ( "invalid" ) ;
@@ -52,3 +101,83 @@ try {
52101}
53102
54103// What other invalid card cases can you think of?
104+ try {
105+ getCardValue ( "11♣" ) ; // testing with "11♣" the error will be thrown because "11" is not a valid rank
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 ( e ) {
110+ console . log ( "Error thrown for invalid card 🎉" ) ;
111+ }
112+
113+ try {
114+ getCardValue ( "A" ) ; // testing with "A" the error will be thrown because there is no suit
115+
116+ // This line will not be reached if an error is thrown as expected
117+ console . error ( "Error was not thrown for invalid card 😢" ) ;
118+ } catch ( e ) {
119+ console . log ( "Error thrown for invalid card 🎉" ) ;
120+ }
121+
122+ try {
123+ getCardValue ( "10" ) ; // testing with "10" the error will be thrown because there is no suit
124+
125+ // This line will not be reached if an error is thrown as expected
126+ console . error ( "Error was not thrown for invalid card 😢" ) ;
127+ } catch ( e ) {
128+ console . log ( "Error thrown for invalid card 🎉" ) ;
129+ }
130+
131+ try {
132+ getCardValue ( "1♣" ) ; // testing with "1♣" the error will be thrown because "1" is not a valid rank
133+
134+ // This line will not be reached if an error is thrown as expected
135+ console . error ( "Error was not thrown for invalid card 😢" ) ;
136+ } catch ( e ) {
137+ console . log ( "Error thrown for invalid card 🎉" ) ;
138+ }
139+
140+ try {
141+ getCardValue ( "A5" ) ; // testing with "A5" the error will be thrown because "5" is not a valid suit
142+
143+ // This line will not be reached if an error is thrown as expected
144+ console . error ( "Error was not thrown for invalid card 😢" ) ;
145+ } catch ( e ) {
146+ console . log ( "Error thrown for invalid card 🎉" ) ;
147+ }
148+
149+ try {
150+ getCardValue ( "♣6" ) ; // testing with "♣6" the error will be thrown because "6" is not a valid suit and "♣" is not a valid rank
151+
152+ // This line will not be reached if an error is thrown as expected
153+ console . error ( "Error was not thrown for invalid card 😢" ) ;
154+ } catch ( e ) {
155+ console . log ( "Error thrown for invalid card 🎉" ) ;
156+ }
157+
158+ try {
159+ getCardValue ( "@@" ) ; // testing with "@@" the error will be thrown because "@" is not a valid rank or suit
160+
161+ // This line will not be reached if an error is thrown as expected
162+ console . error ( "Error was not thrown for invalid card 😢" ) ;
163+ } catch ( e ) {
164+ console . log ( "Error thrown for invalid card 🎉" ) ;
165+ }
166+
167+ try {
168+ getCardValue ( "-6♣" ) ; // testing with "-6♣" (negative number) the error will be thrown because "-" is not a valid rank
169+
170+ // This line will not be reached if an error is thrown as expected
171+ console . error ( "Error was not thrown for invalid card 😢" ) ;
172+ } catch ( e ) {
173+ console . log ( "Error thrown for invalid card 🎉" ) ;
174+ }
175+
176+ try {
177+ getCardValue ( "" ) ; // testing with "" the error will be thrown because "" is not a valid card
178+
179+ // This line will not be reached if an error is thrown as expected
180+ console . error ( "Error was not thrown for invalid card 😢" ) ;
181+ } catch ( e ) {
182+ console . log ( "Error thrown for invalid card 🎉" ) ;
183+ }
0 commit comments