Skip to content

Commit 3afdd04

Browse files
committed
get card value file update
1 parent 25fc120 commit 3afdd04

1 file changed

Lines changed: 41 additions & 0 deletions

File tree

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

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,32 @@
2323

2424
function getCardValue(card) {
2525
// TODO: Implement this function
26+
if (typeof card !== "string" || card.length < 2) {
27+
throw new Error("Invalid card format");
28+
}
29+
const rank = card.slice(0, -1);
30+
const suit = card.slice(-1);
31+
32+
const validSuits = ["♠", "♥", "♦", "♣"];
33+
if (!validSuits.includes(suit)) {
34+
throw new Error("Invalid suit");
35+
}
36+
if (rank === "A") {
37+
return 11;
38+
}
39+
if (["J", "Q", "K"].includes(rank)) {
40+
return 10;
41+
}
42+
const numericValue = parseInt(rank, 10);
43+
if (
44+
!isNaN(numericValue) &&
45+
numericValue >= 2 &&
46+
numericValue <= 10 &&
47+
String(numericValue) === rank
48+
) {
49+
return numericValue;
50+
}
51+
throw new Error("Invalid rank");
2652
}
2753

2854
// The line below allows us to load the getCardValue function into tests in other files.
@@ -52,3 +78,18 @@ try {
5278
}
5379

5480
// What other invalid card cases can you think of?
81+
assertEquals(getCardValue("9♠"), 9);
82+
assertEquals(getCardValue("2♥"), 2);
83+
assertEquals(getCardValue("10♥"), 10);
84+
assertEquals(getCardValue("A♣"), 11);
85+
assertEquals(getCardValue("J♦"), 10);
86+
assertEquals(getCardValue("Q♠"), 10);
87+
assertEquals(getCardValue("K♣"), 10);
88+
89+
assertThrows("invalid");
90+
assertThrows("A");
91+
assertThrows("♠");
92+
assertThrows("1♥");
93+
assertThrows("Z♠");
94+
assertThrows("10");
95+
assertThrows("A♥♠");

0 commit comments

Comments
 (0)