File tree Expand file tree Collapse file tree 2 files changed +40
-3
lines changed
Sprint-3/1-implement-and-rewrite-tests Expand file tree Collapse file tree 2 files changed +40
-3
lines changed Original file line number Diff line number Diff line change 2323
2424function getCardValue ( card ) {
2525 // TODO: Implement this function
26+
27+ // if card is an ace return 11
28+ if ( card === "A" ) {
29+ return 11 ;
30+ }
31+
32+ // if card is face "J", "Q", "K"
33+ else if ( card === "J" || card === "Q" || card === "K" ) {
34+ return 10 ;
35+ }
36+
37+ // if card is a number card and card is bigger than 2 and less than 10 ("2" to "10"), should return its numerical value
38+ else if ( Number ( card ) >= 2 && Number ( card ) <= 10 ) {
39+ return Number ( card ) ;
40+ } else {
41+ throw new Error ( "Error invalid card" ) ;
42+ }
2643}
2744
2845// The line below allows us to load the getCardValue function into tests in other files.
@@ -39,7 +56,7 @@ function assertEquals(actualOutput, targetOutput) {
3956
4057// TODO: Write tests to cover all outcomes, including throwing errors for invalid cards.
4158// Examples:
42- assertEquals ( getCardValue ( "9♠ " ) , 9 ) ;
59+ assertEquals ( getCardValue ( "9" ) , 9 ) ;
4360
4461// Handling invalid cards
4562try {
Original file line number Diff line number Diff line change @@ -6,15 +6,35 @@ const getCardValue = require("../implement/3-get-card-value");
66
77// Case 1: Ace (A)
88test ( `Should return 11 when given an ace card` , ( ) => {
9- expect ( getCardValue ( "A♠ " ) ) . toEqual ( 11 ) ;
9+ expect ( getCardValue ( "A" ) ) . toEqual ( 11 ) ;
1010} ) ;
1111
1212// Suggestion: Group the remaining test data into these categories:
1313// Number Cards (2-10)
1414// Face Cards (J, Q, K)
1515// Invalid Cards
1616
17+ // Case 2: Face (J, Q, K)
18+ test ( `Should return 10 when given an face card` , ( ) => {
19+ expect ( getCardValue ( "J" ) ) . toEqual ( 10 ) ;
20+ } ) ;
21+
22+ test ( `Should return 10 when given an face card` , ( ) => {
23+ expect ( getCardValue ( "Q" ) ) . toEqual ( 10 ) ;
24+ } ) ;
25+
26+ test ( `Should return 10 when given an face card` , ( ) => {
27+ expect ( getCardValue ( "K" ) ) . toEqual ( 10 ) ;
28+ } ) ;
29+ // Case 3: Number Cards (2-10)
30+ test ( `Should return numerical value when given an number card` , ( ) => {
31+ expect ( getCardValue ( "5" ) ) . toEqual ( 5 ) ;
32+ } ) ;
33+
34+ test ( `Should return error if the card string is invalid` , ( ) => {
35+ expect ( ( ) => getCardValue ( "17" ) ) . toThrow ( "Error invalid card" ) ;
36+ } ) ;
37+
1738// To learn how to test whether a function throws an error as expected in Jest,
1839// please refer to the Jest documentation:
1940// https://jestjs.io/docs/expect#tothrowerror
20-
You can’t perform that action at this time.
0 commit comments