Skip to content

Commit 01d5575

Browse files
committed
Implement getCardValue and add tests
1 parent 3d7735d commit 01d5575

2 files changed

Lines changed: 54 additions & 6 deletions

File tree

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

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

2424
function getCardValue(card) {
25-
// TODO: Implement this function
25+
const validRanks = ["A", "2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K"];
26+
const validSuits = ["♠", "♥", "♦", "♣"];
27+
28+
let rank;
29+
let suit;
30+
31+
if (card.startsWith("10")) {
32+
rank = "10";
33+
suit = card.slice(2);
34+
} else {
35+
rank = card.slice(0, 1);
36+
suit = card.slice(1);
37+
}
38+
39+
if (!validRanks.includes(rank) || !validSuits.includes(suit)) {
40+
throw new Error(`Invalid card: ${card}`);
41+
}
42+
43+
if (rank === "A") {
44+
return 11;
45+
} else if (rank === "J" || rank === "Q" || rank === "K") {
46+
return 10;
47+
} else {
48+
return Number(rank);
49+
}
2650
}
2751

2852
// The line below allows us to load the getCardValue function into tests in other files.
@@ -40,15 +64,18 @@ function assertEquals(actualOutput, targetOutput) {
4064
// TODO: Write tests to cover all outcomes, including throwing errors for invalid cards.
4165
// Examples:
4266
assertEquals(getCardValue("9♠"), 9);
67+
assertEquals(getCardValue("A♥"), 11);
68+
assertEquals(getCardValue("K♦"), 10);
69+
assertEquals(getCardValue("10♣"), 10);
4370

4471
// Handling invalid cards
4572
try {
4673
getCardValue("invalid");
4774

4875
// This line will not be reached if an error is thrown as expected
49-
console.error("Error was not thrown for invalid card 😢");
76+
console.error("Error was not thrown for invalid card 😞");
5077
} catch (e) {
5178
console.log("Error thrown for invalid card 🎉");
5279
}
5380

54-
// What other invalid card cases can you think of?
81+
// What other invalid card cases can you think of?

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

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,37 @@ const getCardValue = require("../implement/3-get-card-value");
55
// TODO: Write tests in Jest syntax to cover all possible outcomes.
66

77
// Case 1: Ace (A)
8-
test(`Should return 11 when given an ace card`, () => {
8+
test("Should return 11 when given an ace card", () => {
99
expect(getCardValue("A♠")).toEqual(11);
1010
});
1111

12+
// Case 2: Number cards (2-10)
13+
test("should return the numeric value for number cards", () => {
14+
expect(getCardValue("2♠")).toEqual(2);
15+
expect(getCardValue("9♥")).toEqual(9);
16+
expect(getCardValue("10♦")).toEqual(10);
17+
});
18+
19+
// Case 3: Face cards (J, Q, K)
20+
test("should return 10 for face cards", () => {
21+
expect(getCardValue("J♣")).toEqual(10);
22+
expect(getCardValue("Q♠")).toEqual(10);
23+
expect(getCardValue("K♥")).toEqual(10);
24+
});
25+
26+
// Case 4: Invalid cards
27+
test("should throw an error for invalid cards", () => {
28+
expect(() => getCardValue("invalid")).toThrow();
29+
expect(() => getCardValue("1♠")).toThrow();
30+
expect(() => getCardValue("A")).toThrow();
31+
expect(() => getCardValue("K")).toThrow();
32+
});
33+
1234
// Suggestion: Group the remaining test data into these categories:
1335
// Number Cards (2-10)
1436
// Face Cards (J, Q, K)
1537
// Invalid Cards
1638

1739
// To learn how to test whether a function throws an error as expected in Jest,
1840
// please refer to the Jest documentation:
19-
// https://jestjs.io/docs/expect#tothrowerror
20-
41+
// https://jestjs.io/docs/expect#tothrowerror

0 commit comments

Comments
 (0)