Skip to content

Commit fc56676

Browse files
committed
Implement getCardValue and add assert and Jest tests
1 parent b1186bd commit fc56676

2 files changed

Lines changed: 60 additions & 8 deletions

File tree

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

Lines changed: 35 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,14 @@
2222
// execute the code to ensure all tests pass.
2323

2424
function getCardValue(card) {
25-
// TODO: Implement this function
25+
let rank = card.slice(0, -1);
26+
let suit = card.slice(-1);
27+
const suitArray = ["♠", "♥", "♦", "♣"];
28+
if (!suitArray.includes(suit)) throw new Error("Invalid card");
29+
if (rank === "A") return 11;
30+
if (rank === "Q" || rank === "K" || rank === "J") return 10;
31+
if (Number(rank) >= 2 && Number(rank) <= 10) return Number(rank);
32+
throw new Error("Invalid card");
2633
}
2734

2835
// The line below allows us to load the getCardValue function into tests in other files.
@@ -39,13 +46,37 @@ function assertEquals(actualOutput, targetOutput) {
3946

4047
// TODO: Write tests to cover all outcomes, including throwing errors for invalid cards.
4148
// Examples:
42-
assertEquals(getCardValue("9♠"), 9);
49+
assertEquals(getCardValue("A♠"), 11);
50+
assertEquals(getCardValue("A♣"), 11);
51+
assertEquals(getCardValue("Q♠"), 10);
52+
assertEquals(getCardValue("K♣"), 10);
53+
assertEquals(getCardValue("J♦"), 10);
54+
assertEquals(getCardValue("2♠"), 2);
55+
assertEquals(getCardValue("10♣"), 10);
4356

4457
// Handling invalid cards
4558
try {
46-
getCardValue("invalid");
59+
getCardValue("AX");
60+
console.error("Error was not thrown for invalid card 😢");
61+
} catch (e) {
62+
console.log("Error thrown for invalid card 🎉");
63+
}
64+
try {
65+
getCardValue("B♣");
66+
console.error("Error was not thrown for invalid card 😢");
67+
} catch (e) {
68+
console.log("Error thrown for invalid card 🎉");
69+
}
70+
71+
try {
72+
getCardValue("1♦");
73+
console.error("Error was not thrown for invalid card 😢");
74+
} catch (e) {
75+
console.log("Error thrown for invalid card 🎉");
76+
}
4777

48-
// This line will not be reached if an error is thrown as expected
78+
try {
79+
getCardValue("11♣");
4980
console.error("Error was not thrown for invalid card 😢");
5081
} catch (e) {
5182
console.log("Error thrown for invalid card 🎉");

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

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,31 @@ const getCardValue = require("../implement/3-get-card-value");
44

55
// TODO: Write tests in Jest syntax to cover all possible outcomes.
66

7-
// Case 1: Ace (A)
8-
test(`Should return 11 when given an ace card`, () => {
9-
expect(getCardValue("A♠")).toEqual(11);
7+
describe("getCardValue", () => {
8+
describe("Valid Card", () => {
9+
test("Ace cards return 11", () => {
10+
expect(getCardValue("A♠")).toEqual(11);
11+
expect(getCardValue("A♣")).toEqual(11);
12+
});
13+
test("Face cards return 10", () => {
14+
expect(getCardValue("Q♠")).toEqual(10);
15+
expect(getCardValue("K♣")).toEqual(10);
16+
expect(getCardValue("J♦")).toEqual(10);
17+
});
18+
test("Number cards return their value", () => {
19+
expect(getCardValue("2♠")).toEqual(2);
20+
expect(getCardValue("10♣")).toEqual(10);
21+
});
22+
});
23+
24+
describe("Invalid Card", () => {
25+
test("Throw error for invalid inputs", () => {
26+
expect(() => getCardValue("AX")).toThrow("Invalid card");
27+
expect(() => getCardValue("B♣")).toThrow("Invalid card");
28+
expect(() => getCardValue("1♦")).toThrow("Invalid card");
29+
expect(() => getCardValue("11♣")).toThrow("Invalid card");
30+
});
31+
});
1032
});
1133

1234
// Suggestion: Group the remaining test data into these categories:
@@ -17,4 +39,3 @@ test(`Should return 11 when given an ace card`, () => {
1739
// To learn how to test whether a function throws an error as expected in Jest,
1840
// please refer to the Jest documentation:
1941
// https://jestjs.io/docs/expect#tothrowerror
20-

0 commit comments

Comments
 (0)