Skip to content

Commit 3d438c6

Browse files
committed
Completed 3-get-card-value
1 parent a73fa1a commit 3d438c6

File tree

2 files changed

+40
-3
lines changed

2 files changed

+40
-3
lines changed

Sprint-3/1-implement-and-rewrite-tests/implement/3-get-card-value.js

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,23 @@
2323

2424
function 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
4562
try {

Sprint-3/1-implement-and-rewrite-tests/rewrite-tests-with-jest/3-get-card-value.test.js

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,35 @@ const getCardValue = require("../implement/3-get-card-value");
66

77
// Case 1: Ace (A)
88
test(`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-

0 commit comments

Comments
 (0)