Skip to content

Commit f582fe6

Browse files
committed
fix: validate card ranks strictly and group tests by category
- Updated 3-get-card-value.js to reject invalid ranks like "0002♠", "0x02♠", and "2.1♠" - Simplified logic with explicit checks for A, J, Q, K, 10, and digits 2–9 - Updated 3-get-card-value.test.js to group tests by categories: • Ace → returns 11 • Number cards (2–10) → return their numeric value • Face cards (J, Q, K) → return 10 • Invalid ranks → throw "Invalid card rank"
1 parent c3ed0d1 commit f582fe6

File tree

2 files changed

+29
-23
lines changed

2 files changed

+29
-23
lines changed

Sprint-3/2-mandatory-rewrite/3-get-card-value.js

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,16 @@
11
function getCardValue(card) {
2-
const rank = card.slice(0, -1); // All characters except the last one (suit)
2+
const rank = card.slice(0, -1); // remove suit (last char)
33

44
if (!rank) throw new Error("Invalid card rank");
55

66
if (rank === "A") return 11;
7-
if (["K", "Q", "J", "10"].includes(rank)) return 10;
7+
if (["K", "Q", "J"].includes(rank)) return 10;
8+
if (rank === "10") return 10;
89

9-
const number = Number(rank);
10-
if (number >= 2 && number <= 9) return number;
10+
// Explicit check for number cards (2–9 only, no leading zeros or decimals)
11+
if (/^[2-9]$/.test(rank)) {
12+
return Number(rank);
13+
}
1114

1215
throw new Error("Invalid card rank");
1316
}
Lines changed: 22 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,36 @@
11
const getCardValue = require("./3-get-card-value");
22

3-
test("should return 11 for Ace of Spades", () => {
3+
// Case 1: Ace
4+
test("should return 11 for Ace", () => {
45
expect(getCardValue("A♠")).toEqual(11);
6+
expect(getCardValue("A♥")).toEqual(11);
57
});
68

7-
// Case 2: Handle Number Cards (2-10)
8-
test("should return 5 for Five of Hearts", () => {
9-
expect(getCardValue("5♥")).toEqual(5);
10-
});
11-
12-
test("should return 10 for Ten of Diamonds", () => {
13-
expect(getCardValue("10♦")).toEqual(10);
14-
});
15-
16-
// Case 3: Handle Face Cards (J, Q, K)
17-
test("should return 10 for Jack of Clubs", () => {
18-
expect(getCardValue("J♣")).toEqual(10);
19-
});
9+
// Case 2: Number cards (2–10)
10+
test("should return the value of number cards (2–10)", () => {
11+
expect(getCardValue("2♣")).toEqual(2);
12+
expect(getCardValue("5♠")).toEqual(5);
13+
expect(getCardValue("10♥")).toEqual(10);
2014

21-
test("should return 10 for Queen of Spades", () => {
22-
expect(getCardValue("Q♠")).toEqual(10);
15+
// You can even loop for efficiency:
16+
for (let i = 2; i <= 10; i++) {
17+
expect(getCardValue(`${i}♦`)).toEqual(i);
18+
}
2319
});
2420

25-
test("should return 10 for King of Hearts", () => {
26-
expect(getCardValue("K♥")).toEqual(10);
21+
// Case 3: Face cards (J, Q, K)
22+
test("should return 10 for face cards (J, Q, K)", () => {
23+
["J", "Q", "K"].forEach(rank => {
24+
expect(getCardValue(`${rank}♠`)).toEqual(10);
25+
expect(getCardValue(`${rank}♥`)).toEqual(10);
26+
});
2727
});
2828

29-
// Case 5: Handle Invalid Cards
29+
// Case 4: Invalid cards
3030
test("should throw an error for invalid card rank", () => {
3131
expect(() => getCardValue("Z♣")).toThrow("Invalid card rank");
32+
expect(() => getCardValue("0x02♠")).toThrow("Invalid card rank");
33+
expect(() => getCardValue("2.1♠")).toThrow("Invalid card rank");
34+
expect(() => getCardValue("0002♠")).toThrow("Invalid card rank");
3235
});
3336

0 commit comments

Comments
 (0)