2222// execute the code to ensure all tests pass.
2323
2424function getCardValue ( card ) {
25- let rankStr = card . slice ( 0 , - 1 ) ;
26- let rank = Number ( rankStr ) ;
27- console . log ( "rank :" + rank ) ;
25+ let rank = card . slice ( 0 , - 1 ) ;
2826 let suit = card . slice ( - 1 ) ;
29- console . log ( "suit :" + suit ) ;
3027 const suitArray = [ "♠" , "♥" , "♦" , "♣" ] ;
31- if ( ! suitArray . includes ( suit ) || ! / ^ ( 1 0 | [ 2 - 9 ] ) $ / . test ( rankStr ) ) {
28+ const rankArray = [
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+ "A" ,
42+ ] ;
43+ if ( ! suitArray . includes ( suit ) || ! rankArray . includes ( rank ) ) {
3244 throw new Error ( "Invalid card" ) ;
3345 } else if ( rank === "A" ) {
3446 return 11 ;
3547 } else if ( rank === "Q" || rank === "K" || rank === "J" ) {
3648 return 10 ;
37- } else if ( rank >= 2 && rank <= 10 && Number . isInteger ( rank ) ) {
49+ } else if ( Number ( rank ) >= 2 && Number ( rank ) <= 10 ) {
3850 return Number ( rank ) ;
39- } else {
40- throw new Error ( "Invalid card" ) ;
4151 }
4252}
4353
44- console . log ( getCardValue ( "0002♠" ) ) ;
45-
4654// The line below allows us to load the getCardValue function into tests in other files.
4755// This will be useful in the "rewrite tests with jest" step.
4856module . exports = getCardValue ;
@@ -57,16 +65,16 @@ function assertEquals(actualOutput, targetOutput) {
5765
5866// TODO: Write tests to cover all outcomes, including throwing errors for invalid cards.
5967// Examples:
60- /* assertEquals(getCardValue("A♠"), 11);
68+ assertEquals ( getCardValue ( "A♠" ) , 11 ) ;
6169assertEquals ( getCardValue ( "A♣" ) , 11 ) ;
6270assertEquals ( getCardValue ( "Q♠" ) , 10 ) ;
6371assertEquals ( getCardValue ( "K♣" ) , 10 ) ;
6472assertEquals ( getCardValue ( "J♦" ) , 10 ) ;
6573assertEquals ( getCardValue ( "2♠" ) , 2 ) ;
66- assertEquals(getCardValue("10♣"), 10);*/
74+ assertEquals ( getCardValue ( "10♣" ) , 10 ) ;
6775
6876// Handling invalid cards
69- /* try {
77+ try {
7078 getCardValue ( "AX" ) ;
7179 console . error ( "Error was not thrown for invalid card 😢" ) ;
7280} catch ( e ) {
9199 console . error ( "Error was not thrown for invalid card 😢" ) ;
92100} catch ( e ) {
93101 console . log ( "Error thrown for invalid card 🎉" ) ;
94- }*/
102+ }
95103
96104// What other invalid card cases can you think of?
0 commit comments