Skip to content

Commit 2e62748

Browse files
committed
Implement getCardValue function with validation and tests for card values and error handling
1 parent 98015f6 commit 2e62748

File tree

2 files changed

+82
-1
lines changed

2 files changed

+82
-1
lines changed

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

Lines changed: 56 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,45 @@
2222
// execute the code to ensure all tests pass.
2323

2424
function getCardValue(card) {
25-
// TODO: Implement this function
25+
const validRanks = [
26+
"A",
27+
"2",
28+
"3",
29+
"4",
30+
"5",
31+
"6",
32+
"7",
33+
"8",
34+
"9",
35+
"10",
36+
"J",
37+
"Q",
38+
"K",
39+
];
40+
const validSuits = ["♠", "♥", "♦", "♣"];
41+
42+
// Extract rank and suit
43+
const suit = card.slice(-1);
44+
const rank = card.slice(0, -1);
45+
46+
// Validate suit
47+
if (!validSuits.includes(suit)) {
48+
throw new Error("Invalid card: invalid suit");
49+
}
50+
51+
// Validate rank
52+
if (!validRanks.includes(rank)) {
53+
throw new Error("Invalid card: invalid rank");
54+
}
55+
56+
// Return the card value
57+
if (rank === "A") {
58+
return 11;
59+
} else if (["J", "Q", "K"].includes(rank)) {
60+
return 10;
61+
} else {
62+
return parseInt(rank);
63+
}
2664
}
2765

2866
// The line below allows us to load the getCardValue function into tests in other files.
@@ -40,6 +78,9 @@ function assertEquals(actualOutput, targetOutput) {
4078
// TODO: Write tests to cover all outcomes, including throwing errors for invalid cards.
4179
// Examples:
4280
assertEquals(getCardValue("9♠"), 9);
81+
assertEquals(getCardValue("5♥"), 5);
82+
assertEquals(getCardValue("K♦"), 10);
83+
assertEquals(getCardValue("A♣"), 11);
4384

4485
// Handling invalid cards
4586
try {
@@ -50,3 +91,17 @@ try {
5091
} catch (e) {}
5192

5293
// What other invalid card cases can you think of?
94+
95+
try {
96+
getCardValue("1♠");
97+
console.error("Error was not thrown for invalid card rank");
98+
} catch (error) {
99+
assertEquals(error.message, "Invalid card rank");
100+
}
101+
102+
try {
103+
getCardValue("5X");
104+
console.error("Error was not thrown for invalid card suit");
105+
} catch (error) {
106+
assertEquals(error.message, "Invalid card suit");
107+
}

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

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,40 @@ const getCardValue = require("../implement/3-get-card-value");
77
// Case 1: Ace (A)
88
test(`Should return 11 when given an ace card`, () => {
99
expect(getCardValue("A♠")).toEqual(11);
10+
expect(getCardValue("A♥")).toEqual(11);
1011
});
1112

1213
// Suggestion: Group the remaining test data into these categories:
1314
// Number Cards (2-10)
1415
// Face Cards (J, Q, K)
1516
// Invalid Cards
1617

18+
test(`Should return the correct numeric value for number cards`, () => {
19+
expect(getCardValue("2♣")).toEqual(2);
20+
expect(getCardValue("7♥")).toEqual(7);
21+
expect(getCardValue("10♦")).toEqual(10);
22+
});
23+
24+
test(`Should return 10 for face cards (J, Q, K)`, () => {
25+
expect(getCardValue("J♠")).toEqual(10);
26+
expect(getCardValue("Q♥")).toEqual(10);
27+
expect(getCardValue("K♦")).toEqual(10);
28+
});
29+
1730
// To learn how to test whether a function throws an error as expected in Jest,
1831
// please refer to the Jest documentation:
1932
// https://jestjs.io/docs/expect#tothrowerror
2033

34+
test(`Should throw an error for invalid card rank`, () => {
35+
expect(() => getCardValue("1♠")).toThrow("Invalid card rank");
36+
expect(() => getCardValue("11♥")).toThrow("Invalid card rank");
37+
expect(() => getCardValue("Z♦")).toThrow("Invalid card rank");
38+
expect(() => getCardValue("0x02♠")).toThrow("Invalid card rank");
39+
expect(() => getCardValue("3.1416♠")).toThrow("Invalid card rank");
40+
});
41+
42+
test(`Should throw an error for invalid card suit`, () => {
43+
expect(() => getCardValue("5X")).toThrow("Invalid card suit");
44+
expect(() => getCardValue("10-")).toThrow("Invalid card suit");
45+
expect(() => getCardValue("A/")).toThrow("Invalid card suit");
46+
});

0 commit comments

Comments
 (0)