Skip to content

Commit 7c5d0b4

Browse files
committed
implement getCardValue() function and add tests
1 parent f8431cf commit 7c5d0b4

1 file changed

Lines changed: 84 additions & 1 deletion

File tree

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

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

2424
function getCardValue(card) {
25-
// TODO: Implement this function
25+
const suits = ["♠", "♥", "♦", "♣"];
26+
if (card[0] === "A" && suits.includes(card[1]) && card.length === 2)
27+
return 11;
28+
if (
29+
(card[0] === "J" || card[0] === "Q" || card[0] === "K") &&
30+
suits.includes(card[1]) &&
31+
card.length === 2
32+
)
33+
return 10;
34+
if (
35+
["2", "3", "4", "5", "6", "7", "8", "9"].includes(card[0]) &&
36+
suits.includes(card[1]) &&
37+
card.length === 2
38+
)
39+
return Number(card[0]);
40+
if (card.slice(0, 2) == "10" && suits.includes(card[2]) && card.length === 3)
41+
return 10;
42+
throw new Error(
43+
'Invalid input: Expected rank followed by a suit symbol. For example: "A♠", "2♥", "10♥", "J♣", "Q♦", "K♦"'
44+
);
2645
}
2746

2847
// The line below allows us to load the getCardValue function into tests in other files.
@@ -40,6 +59,11 @@ function assertEquals(actualOutput, targetOutput) {
4059
// TODO: Write tests to cover all outcomes, including throwing errors for invalid cards.
4160
// Examples:
4261
assertEquals(getCardValue("9♠"), 9);
62+
assertEquals(getCardValue("Q♦"), 10);
63+
assertEquals(getCardValue("K♣"), 10);
64+
assertEquals(getCardValue("10♥"), 10);
65+
assertEquals(getCardValue("2♦"), 2);
66+
assertEquals(getCardValue("7♦"), 7);
4367

4468
// Handling invalid cards
4569
try {
@@ -52,3 +76,62 @@ try {
5276
}
5377

5478
// What other invalid card cases can you think of?
79+
// for instance "1♦" where it starts with 1 but its not 10
80+
// where it includes the correct answer but there are added characters maybe space(s) like "9♠ "
81+
//where its the opposite way : "♠9"
82+
//where it starts with a valid number but has otherwise more digits eg: "9♠♠" or "99♠"
83+
//where there is a different symbol from the 4 given such as ❤️
84+
85+
try {
86+
getCardValue("1♥");
87+
88+
// This line will not be reached if an error is thrown as expected
89+
console.error("Error was not thrown for invalid card 😢");
90+
} catch (e) {
91+
console.log("Error thrown for invalid card 🎉");
92+
}
93+
94+
try {
95+
getCardValue("9♠ ");
96+
97+
// This line will not be reached if an error is thrown as expected
98+
console.error("Error was not thrown for invalid card 😢");
99+
} catch (e) {
100+
console.log("Error thrown for invalid card 🎉");
101+
}
102+
103+
try {
104+
getCardValue("♠9");
105+
106+
// This line will not be reached if an error is thrown as expected
107+
console.error("Error was not thrown for invalid card 😢");
108+
} catch (e) {
109+
console.log("Error thrown for invalid card 🎉");
110+
}
111+
112+
try {
113+
getCardValue("99♠");
114+
115+
// This line will not be reached if an error is thrown as expected
116+
console.error("Error was not thrown for invalid card 😢");
117+
} catch (e) {
118+
console.log("Error thrown for invalid card 🎉");
119+
}
120+
121+
try {
122+
getCardValue("9♠♠");
123+
124+
// This line will not be reached if an error is thrown as expected
125+
console.error("Error was not thrown for invalid card 😢");
126+
} catch (e) {
127+
console.log("Error thrown for invalid card 🎉");
128+
}
129+
130+
try {
131+
getCardValue("9❤️");
132+
133+
// This line will not be reached if an error is thrown as expected
134+
console.error("Error was not thrown for invalid card 😢");
135+
} catch (e) {
136+
console.log("Error thrown for invalid card 🎉");
137+
}

0 commit comments

Comments
 (0)