77// complete the rest of the tests and cases
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
10+ const assert = require ( "assert" ) ;
1011function getCardValue ( card ) {
1112 let rank = card . slice ( 0 , - 1 )
1213 if ( rank === "A" ) {
@@ -15,9 +16,10 @@ function getCardValue(card) {
1516 if ( rank >= 2 && rank <= 9 ) {
1617 return + rank
1718 }
18- if ( rank === "J" ) {
19+ if ( / ^ ( 1 0 | J | Q | K ) $ / . test ( rank ) ) {
1920 return 10
2021 }
22+ throw new Error ( "Invalid card" ) ;
2123}
2224
2325// The line below allows us to load the getCardValue function into tests in other files.
@@ -53,15 +55,25 @@ assertEquals(fiveOfHearts,5)
5355// Given a card with a rank of "10," "J," "Q," or "K",
5456// When the function is called with such a card,
5557// Then it should return the value 10, as these cards are worth 10 points each in blackjack.
56- const faceCards = getCardValue ( "J ♠" ) ;
58+ const faceCards = getCardValue ( "K ♠" ) ;
5759assertEquals ( faceCards , 10 )
60+ const faceCards1 = getCardValue ( "10♠" ) ;
61+ assertEquals ( faceCards1 , 10 ) ;
62+
5863
5964// Handle Ace (A):
6065// Given a card with a rank of "A",
6166// When the function is called with an Ace,
6267// Then it should, by default, assume the Ace is worth 11 points, which is a common rule in blackjack.
68+ const aceOfSpades1 = getCardValue ( "A♠" ) ;
69+ assertEquals ( aceOfSpades1 , 11 ) ;
6370
6471// Handle Invalid Cards:
6572// Given a card with an invalid rank (neither a number nor a recognized face card),
6673// When the function is called with such a card,
6774// Then it should throw an error indicating "Invalid card rank."
75+
76+
77+ assert . throws ( ( ) => getCardValue ( "L♠" ) , Error , / I n v a l i d c a r d / ) ;
78+ assert . throws ( ( ) => getCardValue ( ) , Error , / I n v a l i d c a r d / ) ;
79+ assert . throws ( ( ) => getCardValue ( [ "A♠" ] ) , Error , / I n v a l i d c a r d / ) ;
0 commit comments