Skip to content

Commit 0db8451

Browse files
committed
implemented and tested getCardValue function in 3-get-card-value.js
1 parent 1b26097 commit 0db8451

File tree

1 file changed

+59
-4
lines changed

1 file changed

+59
-4
lines changed

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

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

2424
function getCardValue(card) {
25-
// TODO: Implement this function
25+
const rankList = [
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 suitList = ["♠", "♥", "♦", "♣"];
41+
42+
const faceCard = rankList.slice(-3);
43+
44+
const cardRank = card.slice(0, -1);
45+
const cardSuit = card.charAt(card.length - 1);
46+
47+
const validCard = rankList.includes(cardRank) && suitList.includes(cardSuit);
48+
49+
if (!validCard) {
50+
throw new Error("Invalid card");
51+
}
52+
53+
if (cardRank === "A") {
54+
return 11;
55+
}
56+
57+
if (faceCard.includes(cardRank)) {
58+
return 10;
59+
}
60+
61+
return Number(cardRank);
2662
}
2763

28-
// The line below allows us to load the getCardValue function into tests in other files.
64+
console.log(getCardValue("J♥"));
65+
66+
// The line below allows us to load the getCard Value function into tests in other files.
2967
// This will be useful in the "rewrite tests with jest" step.
3068
module.exports = getCardValue;
3169

@@ -37,9 +75,11 @@ function assertEquals(actualOutput, targetOutput) {
3775
);
3876
}
3977

40-
// TODO: Write tests to cover all outcomes, including throwing errors for invalid cards.
41-
// Examples:
78+
4279
assertEquals(getCardValue("9♠"), 9);
80+
assertEquals(getCardValue("A♥"), 11);
81+
assertEquals(getCardValue("K♦"), 10);
82+
assertEquals(getCardValue("10♣"), 10);
4383

4484
// Handling invalid cards
4585
try {
@@ -49,4 +89,19 @@ try {
4989
console.error("Error was not thrown for invalid card");
5090
} catch (e) {}
5191

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

0 commit comments

Comments
 (0)