Skip to content

Commit 7ee037c

Browse files
committed
Reimplement getCardValue with an array
1 parent 0c95abd commit 7ee037c

2 files changed

Lines changed: 23 additions & 15 deletions

File tree

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

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

2424
function getCardValue(card) {
25-
let rankStr = card.slice(0, -1);
26-
let rank = Number(rankStr);
27-
console.log("rank :" + rank);
25+
let rank = card.slice(0, -1);
2826
let suit = card.slice(-1);
29-
console.log("suit :" + suit);
3027
const suitArray = ["♠", "♥", "♦", "♣"];
31-
if (!suitArray.includes(suit) || !/^(10|[2-9])$/.test(rankStr)) {
28+
const rankArray = [
29+
"2",
30+
"3",
31+
"4",
32+
"5",
33+
"6",
34+
"7",
35+
"8",
36+
"9",
37+
"10",
38+
"J",
39+
"Q",
40+
"K",
41+
"A",
42+
];
43+
if (!suitArray.includes(suit) || !rankArray.includes(rank)) {
3244
throw new Error("Invalid card");
3345
} else if (rank === "A") {
3446
return 11;
3547
} else if (rank === "Q" || rank === "K" || rank === "J") {
3648
return 10;
37-
} else if (rank >= 2 && rank <= 10 && Number.isInteger(rank)) {
49+
} else if (Number(rank) >= 2 && Number(rank) <= 10) {
3850
return Number(rank);
39-
} else {
40-
throw new Error("Invalid card");
4151
}
4252
}
4353

44-
console.log(getCardValue("0002♠"));
45-
4654
// The line below allows us to load the getCardValue function into tests in other files.
4755
// This will be useful in the "rewrite tests with jest" step.
4856
module.exports = getCardValue;
@@ -57,16 +65,16 @@ function assertEquals(actualOutput, targetOutput) {
5765

5866
// TODO: Write tests to cover all outcomes, including throwing errors for invalid cards.
5967
// Examples:
60-
/*assertEquals(getCardValue("A♠"), 11);
68+
assertEquals(getCardValue("A♠"), 11);
6169
assertEquals(getCardValue("A♣"), 11);
6270
assertEquals(getCardValue("Q♠"), 10);
6371
assertEquals(getCardValue("K♣"), 10);
6472
assertEquals(getCardValue("J♦"), 10);
6573
assertEquals(getCardValue("2♠"), 2);
66-
assertEquals(getCardValue("10♣"), 10);*/
74+
assertEquals(getCardValue("10♣"), 10);
6775

6876
// Handling invalid cards
69-
/*try {
77+
try {
7078
getCardValue("AX");
7179
console.error("Error was not thrown for invalid card 😢");
7280
} catch (e) {
@@ -91,6 +99,6 @@ try {
9199
console.error("Error was not thrown for invalid card 😢");
92100
} catch (e) {
93101
console.log("Error thrown for invalid card 🎉");
94-
}*/
102+
}
95103

96104
// 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: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ describe("getCardValue", () => {
1818
test("Number cards return their value", () => {
1919
expect(getCardValue("2♠")).toEqual(2);
2020
expect(getCardValue("10♣")).toEqual(10);
21-
expect(getCardValue("0002♣")).toEqual(2);
2221
});
2322
});
2423

@@ -30,6 +29,7 @@ describe("getCardValue", () => {
3029
expect(() => getCardValue("11♣")).toThrow("Invalid card");
3130
expect(() => getCardValue("0x02♠")).toThrow("Invalid card");
3231
expect(() => getCardValue("2.1♠")).toThrow("Invalid card");
32+
expect(() => getCardValue("0002♠")).toThrow("Invalid card");
3333
});
3434
});
3535
});

0 commit comments

Comments
 (0)