File tree Expand file tree Collapse file tree 1 file changed +11
-12
lines changed
Sprint-3/1-implement-and-rewrite-tests/implement Expand file tree Collapse file tree 1 file changed +11
-12
lines changed Original file line number Diff line number Diff line change 88// write one test at a time, and make it pass, build your solution up methodically
99// just make one change at a time -- don't rush -- programmers are deep and careful thinkers
1010function getCardValue ( card ) {
11- const rank = card . slice ( 0 , - 1 ) ; // remove the suit symbol
11+ const rank = card . slice ( 0 , - 1 ) ;
1212
1313 if ( rank === "A" ) {
1414 return 11 ;
15- }
16-
17- if ( [ "K" , "Q" , "J" , "10" ] . includes ( rank ) ) {
15+ } else if ( rank === "K" || rank === "Q" || rank === "J" || rank === "10" ) {
1816 return 10 ;
19- }
17+ } else {
18+ const numericRank = parseInt ( rank , 10 ) ;
2019
21- const numeric = parseInt ( rank ) ;
22- if ( ! isNaN ( numeric ) ) {
23- return numeric ;
20+ if ( ! isNaN ( numericRank ) && numericRank >= 2 && numericRank <= 9 ) {
21+ return numericRank ;
22+ } else {
23+ throw new Error ( `Invalid or unrecognized card rank: "${ rank } "` ) ;
24+ }
2425 }
25-
26- throw new Error ( "Invalid card rank." ) ;
2726}
2827
2928// The line below allows us to load the getCardValue function into tests in other files.
8887 console . assert ( false , "Expected an error for invalid card rank" ) ;
8988} catch ( error ) {
9089 console . assert (
91- error . message === " Invalid card rank." ,
92- `Expected error message " Invalid card rank." , got "${ error . message } "`
90+ error . message === ' Invalid or unrecognized card rank: "Z"' ,
91+ `Expected error message ' Invalid or unrecognized card rank: "Z"' , got "${ error . message } "`
9392 ) ;
9493}
You can’t perform that action at this time.
0 commit comments