Skip to content

Commit 7d502dc

Browse files
committed
Implement getCardValue function and added tests for valid and invalid inputs
1 parent 6b4ee50 commit 7d502dc

File tree

1 file changed

+50
-0
lines changed

1 file changed

+50
-0
lines changed

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

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

2424
function getCardValue(card) {
2525
// TODO: Implement this function
26+
if (
27+
card.length < 2 ||
28+
card.length > 3 ||
29+
!["♠", "♥", "♦", "♣"].includes(card.slice(-1))
30+
)
31+
throw new Error("invalid suit");
32+
if (card[0] === "A") return 11;
33+
if (["J", "Q", "K"].includes(card[0])) return 10;
34+
if (["2", "3", "4", "5", "6", "7", "8", "9", "10"].includes(card[0]))
35+
return card[0]; // the parseint() or Number() can be used to convert the string to a number
36+
throw new Error("invalid rank");
2637
}
2738

2839
// The line below allows us to load the getCardValue function into tests in other files.
@@ -41,6 +52,11 @@ function assertEquals(actualOutput, targetOutput) {
4152
// Examples:
4253
assertEquals(getCardValue("9♠"), 9);
4354

55+
assertEquals(getCardValue("A♥"), 11);
56+
assertEquals(getCardValue("J♣"), 10);
57+
assertEquals(getCardValue("Q♠"), 10);
58+
assertEquals(getCardValue("2♦"), 2);
59+
4460
// Handling invalid cards
4561
try {
4662
getCardValue("invalid");
@@ -50,3 +66,37 @@ try {
5066
} catch (e) {}
5167

5268
// What other invalid card cases can you think of?
69+
try {
70+
getCardValue("S");
71+
console.error("Error was not thrown for invalid card");
72+
} catch (e) {}
73+
74+
try {
75+
getCardValue("AJKP");
76+
console.error("Error was not thrown for invalid card");
77+
} catch (e) {}
78+
79+
try {
80+
getCardValue("A❦");
81+
console.error("Error was not thrown for invalid card");
82+
} catch (e) {}
83+
84+
try {
85+
getCardValue("29");
86+
console.error("Error was not thrown for invalid card");
87+
} catch (e) {}
88+
89+
try {
90+
getCardValue("♦,♣");
91+
console.error("Error was not thrown for invalid card");
92+
} catch (e) {}
93+
94+
try {
95+
getCardValue("2345");
96+
console.error("Error was not thrown for invalid card");
97+
} catch (e) {}
98+
99+
try {
100+
getCardValue("");
101+
console.error("Error was not thrown for invalid card");
102+
} catch (e) {}

0 commit comments

Comments
 (0)