Skip to content

Commit 526ac93

Browse files
implement getCardValue function to return numerical values for playing cards
1 parent cd8ba7b commit 526ac93

1 file changed

Lines changed: 24 additions & 1 deletion

File tree

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

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,5 +24,28 @@
2424
// execute the code to ensure all tests pass.
2525

2626
export function getCardValue(card) {
27-
// TODO: Implement this function
27+
if (typeof card !== "string") {
28+
throw new Error(`Expected a string, but got ${typeof card}`);
29+
}
30+
31+
const cardRegex = /^(10|[2-9]|[AJQK])([♠♥♦♣])$/;
32+
const match = card.match(cardRegex);
33+
34+
if (!match) {
35+
throw new Error(
36+
`Expected a card rank followed by a suit, but got "${card}"`
37+
);
38+
}
39+
40+
const rank = match[1];
41+
42+
if (rank === "A") {
43+
return 11;
44+
}
45+
46+
if (["J", "Q", "K"].includes(rank)) {
47+
return 10;
48+
}
49+
50+
return Number(rank);
2851
}

0 commit comments

Comments
 (0)