2222// execute the code to ensure all tests pass.
2323
2424function getCardValue ( card ) {
25- // Basic validation: validating the input before slicing
26- if ( card === "" ) {
27- throw new Error ( "No card was played" )
28- }
29- if ( card . length < 2 || card . length > 3 ) {
30- throw new Error ( "Invalid card played, rank and suit cannot be less than 1 or more than 3" )
31- }
32- const rank = card . slice ( 0 , card . length - 1 ) . toUpperCase ( )
33- const suit = card . slice ( card . length - 1 )
34-
35- const validSuits = [ "♠" , "♥" , "♦" , "♣" ] ;
36- const validRanks = [ "A" , "2" , "3" , "4" , "5" , "6" , "7" , "8" , "9" , "10" , "J" , "Q" , "K" ] ;
37-
38- // Suit and rank validation
39- if ( ! validSuits . includes ( suit ) ) {
40- throw new Error ( "Invalid card played, suit is missing" ) ;
41- }
42- if ( validRanks . includes ( rank ) ) {
43- if ( rank === "A" ) {
44- return 11 ;
45- } else if ( rank === "J" || rank === "Q" || rank === "K" ) {
46- return 10 ;
47- } else {
48- return Number ( rank ) ;
49- }
25+ // Basic validation: validating the input before slicing
26+ if ( card === "" ) {
27+ throw new Error ( "No card was played" ) ;
28+ }
29+ if ( card . length < 2 || card . length > 3 ) {
30+ throw new Error (
31+ "Invalid card played, rank and suit cannot be less than 1 or more than 3"
32+ ) ;
33+ }
34+ const rank = card . slice ( 0 , - 1 ) . toUpperCase ( ) ;
35+ const suit = card . slice ( - 1 ) ;
36+
37+ const validSuits = [ "♠" , "♥" , "♦" , "♣" ] ;
38+ const validRanks = [
39+ "A" ,
40+ "2" ,
41+ "3" ,
42+ "4" ,
43+ "5" ,
44+ "6" ,
45+ "7" ,
46+ "8" ,
47+ "9" ,
48+ "10" ,
49+ "J" ,
50+ "Q" ,
51+ "K" ,
52+ ] ;
53+
54+ // Suit and rank validation
55+ if ( ! validSuits . includes ( suit ) ) {
56+ throw new Error ( "Invalid card played, suit is missing" ) ;
57+ }
58+ if ( validRanks . includes ( rank ) ) {
59+ if ( rank === "A" ) {
60+ return 11 ;
61+ } else if ( rank === "J" || rank === "Q" || rank === "K" ) {
62+ return 10 ;
5063 } else {
51- throw new Error ( "Invalid rank" ) ;
64+ return Number ( rank ) ;
5265 }
53-
66+ } else {
67+ throw new Error ( "Invalid rank" ) ;
68+ }
5469}
5570
56-
5771// The line below allows us to load the getCardValue function into tests in other files.
5872// This will be useful in the "rewrite tests with jest" step.
5973module . exports = getCardValue ;
6074
6175// Helper functions to make our assertions easier to read.
6276function assertEquals ( actualOutput , targetOutput ) {
63- console . assert (
64- actualOutput === targetOutput ,
65- `Expected ${ actualOutput } to equal ${ targetOutput } `
66- ) ;
77+ console . assert (
78+ actualOutput === targetOutput ,
79+ `Expected ${ actualOutput } to equal ${ targetOutput } `
80+ ) ;
6781}
6882
6983// Examples:
@@ -86,40 +100,38 @@ assertEquals(getCardValue("K♦"), 10);
86100
87101// Handling invalid cards
88102try {
89- getCardValue ( "" ) ;
103+ getCardValue ( "" ) ;
90104
91- // This line will not be reached if an error is thrown as expected
92- console . error ( "Error was not thrown for invalid card 😢" ) ;
105+ // This line will not be reached if an error is thrown as expected
106+ console . error ( "Error was not thrown for invalid card 😢" ) ;
93107} catch ( e ) {
94- console . log ( e ) ;
108+ console . log ( e ) ;
95109}
96110
97111// What other invalid card cases can you think of?
98112try {
99- getCardValue ( "100" ) ;
100- console . error ( "Error was not thrown for card with more than 3 in length" ) ;
113+ getCardValue ( "100" ) ;
114+ console . error ( "Error was not thrown for card with more than 3 in length" ) ;
101115} catch ( e ) {
102- console . log ( e )
116+ console . log ( e ) ;
103117}
104118try {
105- getCardValue ( "1" )
106- console . error ( "Error was not thrown for a card.lenght = 1" )
119+ getCardValue ( "1" ) ;
120+ console . error ( "Error was not thrown for a card.lenght = 1" ) ;
107121} catch ( e ) {
108- console . log ( e )
122+ console . log ( e ) ;
109123}
110124
111125try {
112- getCardValue ( "♦" )
113- console . error ( "Error was not thrown for a card play of just suits" )
126+ getCardValue ( "♦" ) ;
127+ console . error ( "Error was not thrown for a card play of just suits" ) ;
114128} catch ( e ) {
115- console . log ( e )
129+ console . log ( e ) ;
116130}
117131
118132try {
119- getCardValue ( "A😊" )
120- console . error ( "Error was not thrown for a card play of a wrong suit" )
133+ getCardValue ( "A😊" ) ;
134+ console . error ( "Error was not thrown for a card play of a wrong suit" ) ;
121135} catch ( e ) {
122- console . log ( e )
136+ console . log ( e ) ;
123137}
124-
125-
0 commit comments