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- let rank = card . slice ( 0 , card . length - 1 )
11+ let rank = card . slice ( 0 , - 1 ) ;
12+ let intPart ;
13+ if ( ! isNaN ( rank ) ) {
14+ intPart = parseInt ( rank ) ;
15+ }
1216 if ( rank === "A" ) {
1317 return 11 ;
1418 }
15- else if ( ( parseInt ( rank ) >= 2 && parseInt ( rank ) ) && parseInt ( rank ) < 11 ) {
19+ else if ( intPart >= 2 && intPart && intPart < 11 ) {
1620 return parseInt ( rank )
1721 }
18- else if ( rank === "J" || rank === "Q" || rank === "K" ) {
22+ else if ( [ "J" , "Q" , "K" ] . includes ( rank ) ) {
1923 return 10 ;
2024 }
2125 else {
22- throw new Error ( "Invalid card rank." )
23- }
24-
25-
26+ throw new Error ( "Invalid card rank." ) ;
27+ }
2628}
2729
2830// The line below allows us to load the getCardValue function into tests in other files.
@@ -76,10 +78,9 @@ assertEquals(aceOfHeart, 11);
7678// Given a card with an invalid rank (neither a number nor a recognized face card),
7779// When the function is called with such a card,
7880// Then it should throw an error indicating "Invalid card rank."
79-
80-
8181try {
82- assertEquals ( getCardValue ( "W♥" ) , "Invalid card rank" ) ;
82+ getCardValue ( "W♥" ) ;
83+ console . log ( "❌ Test failed: no error thrown" ) ;
8384} catch ( error ) {
84- console . log ( error . message ) ;
85+ assertEquals ( error . message , "Invalid card rank." ) ;
8586}
0 commit comments