Skip to content

Commit 7b922da

Browse files
I changed the function and the test cases
1 parent bbf99ca commit 7b922da

2 files changed

Lines changed: 57 additions & 43 deletions

File tree

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

Lines changed: 23 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -25,25 +25,36 @@ function getCardValue(card) {
2525
const suit = card.slice(-1);
2626
const rank = card.slice(0, -1);
2727

28-
if (suit !== "♠" && suit !== "♥" && suit !== "♦" && suit !== "♣") {
28+
const validSuits = ["♠", "♥", "♦", "♣"];
29+
if (!validSuits.includes(suit)) {
2930
throw new Error("Invalid card");
3031
}
3132

32-
if (rank === "A") {
33-
return 11;
33+
const validRanks = [
34+
"A",
35+
"2",
36+
"3",
37+
"4",
38+
"5",
39+
"6",
40+
"7",
41+
"8",
42+
"9",
43+
"10",
44+
"J",
45+
"Q",
46+
"K",
47+
];
48+
49+
if (!validRanks.includes(rank)) {
50+
throw new Error("Invalid card");
3451
}
3552

36-
if (rank === "J" || rank === "Q" || rank === "K") {
37-
return 10;
38-
}
53+
if (rank === "A") return 11;
54+
if (rank === "J" || rank === "Q" || rank === "K") return 10;
3955

40-
if (Number(rank) >= 2 && Number(rank) <= 10) {
41-
return Number(rank);
42-
}
43-
44-
throw new Error("Invalid card");
56+
return Number(rank);
4557
}
46-
4758
// TODO: Implement this function
4859

4960
// The line below allows us to load the getCardValue function into tests in other files.
Lines changed: 34 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,43 +1,46 @@
1-
// This statement loads the isProperFraction function you wrote in the implement directory.
2-
// We will use the same function, but write tests for it using Jest in this file.
3-
const isProperFraction = require("../implement/2-is-proper-fraction");
1+
const getCardValue = require("../implement/3-get-card-value");
42

5-
// denominator is zero
6-
test("should return false when denominator is zero", () => {
7-
expect(isProperFraction(1, 0)).toEqual(false);
3+
// Ace
4+
test("Should return 11 when given an ace card", () => {
5+
expect(getCardValue("A♠")).toBe(11);
86
});
97

10-
// proper fraction (numerator < denominator)
11-
test("should return true when numerator < denominator", () => {
12-
expect(isProperFraction(1, 2)).toEqual(true);
8+
// Number cards
9+
test("Should return the numeric value for number cards", () => {
10+
expect(getCardValue("2♠")).toBe(2);
11+
expect(getCardValue("5♥")).toBe(5);
12+
expect(getCardValue("9♦")).toBe(9);
13+
expect(getCardValue("10♣")).toBe(10);
1314
});
1415

15-
// improper fraction (numerator > denominator)
16-
test("should return false when numerator > denominator", () => {
17-
expect(isProperFraction(5, 3)).toEqual(false);
16+
// Face cards
17+
test("Should return 10 for face cards", () => {
18+
expect(getCardValue("J♠")).toBe(10);
19+
expect(getCardValue("Q♥")).toBe(10);
20+
expect(getCardValue("K♦")).toBe(10);
1821
});
1922

20-
// equal numbers
21-
test("should return false when numerator === denominator", () => {
22-
expect(isProperFraction(4, 4)).toEqual(false);
23+
// Invalid cards (basic)
24+
test("Should throw error for invalid cards", () => {
25+
expect(() => getCardValue("1♠")).toThrow();
26+
expect(() => getCardValue("B♣")).toThrow();
27+
expect(() => getCardValue("10?")).toThrow();
28+
expect(() => getCardValue("invalid")).toThrow();
2329
});
2430

25-
// numerator is zero
26-
test("should return true when numerator is zero and denominator is positive", () => {
27-
expect(isProperFraction(0, 5)).toEqual(true);
31+
// Invalid numeric formats (IMPORTANT for your strict function)
32+
test("Should throw error for malformed numeric ranks", () => {
33+
expect(() => getCardValue("0x02♠")).toThrow();
34+
expect(() => getCardValue("2.1♠")).toThrow();
35+
expect(() => getCardValue("0002♠")).toThrow();
2836
});
2937

30-
// negative numerator
31-
test("should return false when numerator is negative", () => {
32-
expect(isProperFraction(-2, 4)).toEqual(false);
33-
});
34-
35-
// negative denominator
36-
test("should return false when denominator is negative", () => {
37-
expect(isProperFraction(5, -4)).toEqual(false);
38-
});
39-
40-
// both negative
41-
test("should return false when both numerator and denominator are negative", () => {
42-
expect(isProperFraction(-3, -5)).toEqual(false);
38+
// Invalid structure cases
39+
test("Should throw error for malformed card structure", () => {
40+
expect(() => getCardValue("")).toThrow();
41+
expect(() => getCardValue("♠")).toThrow();
42+
expect(() => getCardValue("10")).toThrow();
43+
expect(() => getCardValue("A♠♠")).toThrow();
44+
expect(() => getCardValue(" 2♠")).toThrow();
45+
expect(() => getCardValue("2♠ ")).toThrow();
4346
});

0 commit comments

Comments
 (0)