Skip to content

Commit e2a6053

Browse files
committed
write getCardValue() function ensuring all tests pass
1 parent 9cc4680 commit e2a6053

File tree

2 files changed

+68
-48
lines changed

2 files changed

+68
-48
lines changed

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

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

2424
function getCardValue(card) {
25-
// TODO: Implement this function
25+
const validSuits = ["♠", "♥", "♦", "♣"];
26+
if (typeof card !== "string")
27+
throw new Error(
28+
`Invalid card format: "${card}". Expected rank followed by suit, for example A♠, 10♥, K♦, or 7♣.`
29+
);
30+
31+
let cardSuit = card.slice(-1);
32+
let cardRank = card.slice(0, -1);
33+
34+
if (validSuits.find((suit) => suit === cardSuit)) {
35+
console.log(cardSuit);
36+
switch (cardRank) {
37+
case "A":
38+
console.log(cardRank, "ace rank");
39+
return 11;
40+
41+
case "J":
42+
case "Q":
43+
case "K":
44+
console.log(cardRank, "face rank");
45+
return 10;
46+
47+
case "2":
48+
case "3":
49+
case "4":
50+
case "5":
51+
case "6":
52+
case "7":
53+
case "8":
54+
case "9":
55+
case "10":
56+
console.log(cardRank, "number rank");
57+
return Number(cardRank);
58+
59+
default:
60+
throw new Error(
61+
`Invalid card format: "${card}". Expected rank followed by suit, for example A♠, 10♥, K♦, or 7♣.`
62+
);
63+
}
64+
} else
65+
throw new Error(
66+
`Invalid card format: "${card}". Expected rank followed by suit, for example A♠, 10♥, K♦, or 7♣.`
67+
);
2668
}
2769

2870
// The line below allows us to load the getCardValue function into tests in other files.
@@ -64,7 +106,7 @@ try {
64106
// This line will not be reached if an error is thrown as expected
65107
console.error("Error was not thrown for invalid card");
66108
} catch (err) {
67-
console.error(error);
109+
console.error(err);
68110
}
69111

70112
// What other invalid card cases can you think of?
@@ -75,7 +117,7 @@ try {
75117
// This line will not be reached if an error is thrown as expected
76118
console.error("Error was not thrown for invalid card");
77119
} catch (err) {
78-
console.error(error);
120+
console.error(err);
79121
}
80122

81123
try {
@@ -84,7 +126,7 @@ try {
84126
// This line will not be reached if an error is thrown as expected
85127
console.error("Error was not thrown for invalid card");
86128
} catch (err) {
87-
console.error(error);
129+
console.error(err);
88130
}
89131

90132
try {
@@ -93,7 +135,7 @@ try {
93135
// This line will not be reached if an error is thrown as expected
94136
console.error("Error was not thrown for invalid card");
95137
} catch (err) {
96-
console.error(error);
138+
console.error(err);
97139
}
98140

99141
try {
@@ -102,7 +144,7 @@ try {
102144
// This line will not be reached if an error is thrown as expected
103145
console.error("Error was not thrown for invalid card");
104146
} catch (err) {
105-
console.error(error);
147+
console.error(err);
106148
}
107149

108150
try {
@@ -111,7 +153,7 @@ try {
111153
// This line will not be reached if an error is thrown as expected
112154
console.error("Error was not thrown for invalid card");
113155
} catch (err) {
114-
console.error(error);
156+
console.error(err);
115157
}
116158

117159
try {
@@ -120,7 +162,7 @@ try {
120162
// This line will not be reached if an error is thrown as expected
121163
console.error("Error was not thrown for invalid card");
122164
} catch (err) {
123-
console.error(error);
165+
console.error(err);
124166
}
125167

126168
try {
@@ -129,7 +171,7 @@ try {
129171
// This line will not be reached if an error is thrown as expected
130172
console.error("Error was not thrown for invalid card");
131173
} catch (err) {
132-
console.error(error);
174+
console.error(err);
133175
}
134176

135177
try {
@@ -138,7 +180,7 @@ try {
138180
// This line will not be reached if an error is thrown as expected
139181
console.error("Error was not thrown for invalid card");
140182
} catch (err) {
141-
console.error(error);
183+
console.error(err);
142184
}
143185

144186
try {
@@ -147,7 +189,7 @@ try {
147189
// This line will not be reached if an error is thrown as expected
148190
console.error("Error was not thrown for invalid card");
149191
} catch (err) {
150-
console.error(error);
192+
console.error(err);
151193
}
152194

153195
try {
@@ -156,7 +198,7 @@ try {
156198
// This line will not be reached if an error is thrown as expected
157199
console.error("Error was not thrown for invalid card");
158200
} catch (err) {
159-
console.error(error);
201+
console.error(err);
160202
}
161203

162204
try {
@@ -165,7 +207,7 @@ try {
165207
// This line will not be reached if an error is thrown as expected
166208
console.error("Error was not thrown for invalid card");
167209
} catch (err) {
168-
console.error(error);
210+
console.error(err);
169211
}
170212

171213
try {
@@ -174,7 +216,7 @@ try {
174216
// This line will not be reached if an error is thrown as expected
175217
console.error("Error was not thrown for invalid card");
176218
} catch (err) {
177-
console.error(error);
219+
console.error(err);
178220
}
179221

180222
try {
@@ -183,5 +225,5 @@ try {
183225
// This line will not be reached if an error is thrown as expected
184226
console.error("Error was not thrown for invalid card");
185227
} catch (err) {
186-
console.error(error);
228+
console.error(err);
187229
}

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

Lines changed: 11 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -34,39 +34,17 @@ test(`Should return 10 when given a face card`, () => {
3434

3535
// Invalid Cards
3636
test(`Should throw an error when given an invalid card`, () => {
37-
expect(() => getCardValue("")).toThrow(
38-
"Invalid card format! Expected rank followed by suit, for example A♠, 10♥, K♦, or 7♣."
39-
);
40-
expect(() => getCardValue("invalid")).toThrow(
41-
"Invalid card format! Expected rank followed by suit, for example A♠, 10♥, K♦, or 7♣."
42-
);
43-
expect(() => getCardValue("11♠")).toThrow(
44-
"Invalid card format! Expected rank followed by suit, for example A♠, 10♥, K♦, or 7♣."
45-
);
46-
expect(() => getCardValue("0♦")).toThrow(
47-
"Invalid card format! Expected rank followed by suit, for example A♠, 10♥, K♦, or 7♣."
48-
);
49-
expect(() => getCardValue("4.8♣")).toThrow(
50-
"Invalid card format! Expected rank followed by suit, for example A♠, 10♥, K♦, or 7♣."
51-
);
52-
expect(() => getCardValue("G♦")).toThrow(
53-
"Invalid card format! Expected rank followed by suit, for example A♠, 10♥, K♦, or 7♣."
54-
);
55-
expect(() => getCardValue("9")).toThrow(
56-
"Invalid card format! Expected rank followed by suit, for example A♠, 10♥, K♦, or 7♣."
57-
);
58-
expect(() => getCardValue("K")).toThrow(
59-
"Invalid card format! Expected rank followed by suit, for example A♠, 10♥, K♦, or 7♣."
60-
);
61-
expect(() => getCardValue(6)).toThrow(
62-
"Invalid card format! Expected rank followed by suit, for example A♠, 10♥, K♦, or 7♣."
63-
);
64-
expect(() => getCardValue("♠3")).toThrow(
65-
"Invalid card format! Expected rank followed by suit, for example A♠, 10♥, K♦, or 7♣."
66-
);
67-
expect(() => getCardValue("9M")).toThrow(
68-
"Invalid card format! Expected rank followed by suit, for example A♠, 10♥, K♦, or 7♣."
69-
);
37+
expect(() => getCardValue("")).toThrow(/Invalid card format/);
38+
expect(() => getCardValue("invalid")).toThrow(/Invalid card format/);
39+
expect(() => getCardValue("11♠")).toThrow(/Invalid card format/);
40+
expect(() => getCardValue("0♦")).toThrow(/Invalid card format/);
41+
expect(() => getCardValue("4.8♣")).toThrow(/Invalid card format/);
42+
expect(() => getCardValue("G♦")).toThrow(/Invalid card format/);
43+
expect(() => getCardValue("9")).toThrow(/Invalid card format/);
44+
expect(() => getCardValue("K")).toThrow(/Invalid card format/);
45+
expect(() => getCardValue(6)).toThrow(/Invalid card format/);
46+
expect(() => getCardValue("♠3")).toThrow(/Invalid card format/);
47+
expect(() => getCardValue("9M")).toThrow(/Invalid card format/);
7048
});
7149

7250
// To learn how to test whether a function throws an error as expected in Jest,

0 commit comments

Comments
 (0)