Skip to content

Commit 2ec1a68

Browse files
committed
card value implementation and test
1 parent 1610b10 commit 2ec1a68

File tree

1 file changed

+39
-1
lines changed

1 file changed

+39
-1
lines changed

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

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

2424
function getCardValue(card) {
25-
// TODO: Implement this function
25+
const rank = [
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 suite = ["♠", "♥", "♦", "♣"];
41+
42+
const cardFace = card.slice(0, -1);
43+
const cardSuit = card[card.length - 1];
44+
45+
if (!rank.includes(cardFace) || !suite.includes(cardSuit)) {
46+
throw new Error("invalid input");
47+
}
48+
49+
if ("A" === cardFace) {
50+
return 11;
51+
}
52+
if (["J", "Q", "K"].includes(cardFace)) {
53+
return 10;
54+
}
55+
56+
return Number(cardFace);
2657
}
2758

2859
// The line below allows us to load the getCardValue function into tests in other files.
@@ -40,6 +71,13 @@ function assertEquals(actualOutput, targetOutput) {
4071
// TODO: Write tests to cover all outcomes, including throwing errors for invalid cards.
4172
// Examples:
4273
assertEquals(getCardValue("9♠"), 9);
74+
assertEquals(getCardValue("A♠"), 11);
75+
assertEquals(getCardValue("J♠"), 10);
76+
assertEquals(getCardValue("K♠"), 10);
77+
assertEquals(getCardValue("Q♠"), 10);
78+
assertEquals(getCardValue("2♠"), 2);
79+
assertEquals(getCardValue("5♠"), 5);
80+
assertEquals(getCardValue("10♠"), 10);
4381

4482
// Handling invalid cards
4583
try {

0 commit comments

Comments
 (0)