Skip to content

Commit b707f60

Browse files
Implement getCardValue function with validation and comprehensive tests for all card types
1 parent 32d92f5 commit b707f60

File tree

1 file changed

+111
-6
lines changed

1 file changed

+111
-6
lines changed

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

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

2424
function getCardValue(card) {
25-
// TODO: Implement this function
25+
// Define valid ranks and suits
26+
const validRanks = ["A", "2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K"];
27+
const validSuits = ["♠", "♥", "♦", "♣"];
28+
29+
// Check if card is a valid string
30+
if (typeof card !== 'string' || card.length === 0) {
31+
throw new Error('Invalid card');
32+
}
33+
34+
// Extract suit (last character)
35+
const suit = card[card.length - 1];
36+
37+
// Extract rank (everything except last character)
38+
const rank = card.slice(0, -1);
39+
40+
// Validate suit
41+
if (!validSuits.includes(suit)) {
42+
throw new Error('Invalid card');
43+
}
44+
45+
// Validate rank
46+
if (!validRanks.includes(rank)) {
47+
throw new Error('Invalid card');
48+
}
49+
50+
// Return value based on rank
51+
if (rank === "A") {
52+
return 11;
53+
} else if (rank === "J" || rank === "Q" || rank === "K") {
54+
return 10;
55+
} else {
56+
return parseInt(rank);
57+
}
2658
}
2759

2860
// The line below allows us to load the getCardValue function into tests in other files.
@@ -38,15 +70,88 @@ function assertEquals(actualOutput, targetOutput) {
3870
}
3971

4072
// TODO: Write tests to cover all outcomes, including throwing errors for invalid cards.
41-
// Examples:
73+
74+
// Test all Ace cards (should return 11)
75+
assertEquals(getCardValue("A♠"), 11);
76+
assertEquals(getCardValue("A♥"), 11);
77+
assertEquals(getCardValue("A♦"), 11);
78+
assertEquals(getCardValue("A♣"), 11);
79+
80+
// Test all face cards (should return 10)
81+
assertEquals(getCardValue("J♠"), 10);
82+
assertEquals(getCardValue("J♥"), 10);
83+
assertEquals(getCardValue("J♦"), 10);
84+
assertEquals(getCardValue("J♣"), 10);
85+
assertEquals(getCardValue("Q♠"), 10);
86+
assertEquals(getCardValue("Q♥"), 10);
87+
assertEquals(getCardValue("Q♦"), 10);
88+
assertEquals(getCardValue("Q♣"), 10);
89+
assertEquals(getCardValue("K♠"), 10);
90+
assertEquals(getCardValue("K♥"), 10);
91+
assertEquals(getCardValue("K♦"), 10);
92+
assertEquals(getCardValue("K♣"), 10);
93+
94+
// Test number cards (should return their numeric value)
95+
assertEquals(getCardValue("2♠"), 2);
96+
assertEquals(getCardValue("3♣"), 3);
97+
assertEquals(getCardValue("4♥"), 4);
98+
assertEquals(getCardValue("5♦"), 5);
99+
assertEquals(getCardValue("6♠"), 6);
100+
assertEquals(getCardValue("7♣"), 7);
101+
assertEquals(getCardValue("8♥"), 8);
42102
assertEquals(getCardValue("9♠"), 9);
103+
assertEquals(getCardValue("10♥"), 10);
43104

44-
// Handling invalid cards
105+
// Handling invalid cards - generic invalid string
45106
try {
46107
getCardValue("invalid");
47-
48-
// This line will not be reached if an error is thrown as expected
49108
console.error("Error was not thrown for invalid card");
50109
} catch (e) {}
51110

52-
// What other invalid card cases can you think of?
111+
// Card without suit
112+
try {
113+
getCardValue("A");
114+
console.error("Error was not thrown for card without suit");
115+
} catch (e) {}
116+
117+
// Card without rank (just a suit)
118+
try {
119+
getCardValue("♠");
120+
console.error("Error was not thrown for card without rank");
121+
} catch (e) {}
122+
123+
// Invalid rank
124+
try {
125+
getCardValue("11♠");
126+
console.error("Error was not thrown for invalid rank '11'");
127+
} catch (e) {}
128+
129+
try {
130+
getCardValue("X♠");
131+
console.error("Error was not thrown for invalid rank 'X'");
132+
} catch (e) {}
133+
134+
// Invalid suit
135+
try {
136+
getCardValue("A♪");
137+
console.error("Error was not thrown for invalid suit");
138+
} catch (e) {}
139+
140+
try {
141+
getCardValue("AH");
142+
console.error("Error was not thrown for invalid suit 'H'");
143+
} catch (e) {}
144+
145+
// Empty string
146+
try {
147+
getCardValue("");
148+
console.error("Error was not thrown for empty string");
149+
} catch (e) {}
150+
151+
// Extra characters
152+
try {
153+
getCardValue("A♠X");
154+
console.error("Error was not thrown for extra characters");
155+
} catch (e) {}
156+
157+
console.log("All tests completed!");

0 commit comments

Comments
 (0)