Skip to content

Commit 0a4e753

Browse files
committed
Implement getCardValue function with tests
1 parent 9210c52 commit 0a4e753

File tree

1 file changed

+28
-3
lines changed

1 file changed

+28
-3
lines changed

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

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,16 @@
88
// write one test at a time, and make it pass, build your solution up methodically
99
// just make one change at a time -- don't rush -- programmers are deep and careful thinkers
1010
function getCardValue(card) {
11+
const rank = card.slice(0, -1);
12+
1113
if (rank === "A") {
1214
return 11;
15+
} else if (["K", "Q", "J", "10"].includes(rank)) {
16+
return 10;
17+
} else if (["2", "3", "4", "5", "6", "7", "8", "9"].includes(rank)) {
18+
return Number(rank);
19+
} else {
20+
throw new Error("Invalid card rank.");
1321
}
1422
}
1523

@@ -31,21 +39,32 @@ function assertEquals(actualOutput, targetOutput) {
3139
// Given a card string in the format "A♠" (representing a card in blackjack - the last character will always be an emoji for a suit, and all characters before will be a number 2-10, or one letter of J, Q, K, A),
3240
// When the function getCardValue is called with this card string as input,
3341
// Then it should return the numerical card value
34-
const aceofSpades = getCardValue("A♠");
35-
assertEquals(aceofSpades, 11);
42+
const aceOfSpades = getCardValue("A♠");
43+
assertEquals(aceOfSpades, 11);
3644

3745
// Handle Number Cards (2-10):
3846
// Given a card with a rank between "2" and "9",
3947
// When the function is called with such a card,
4048
// Then it should return the numeric value corresponding to the rank (e.g., "5" should return 5).
41-
const fiveofHearts = getCardValue("5♥");
49+
const fiveOfHearts = getCardValue("5♥");
50+
assertEquals(fiveOfHearts, 5);
4251
// ====> write your test here, and then add a line to pass the test in the function above
4352

4453
// Handle Face Cards (J, Q, K):
4554
// Given a card with a rank of "10," "J," "Q," or "K",
4655
// When the function is called with such a card,
4756
// Then it should return the value 10, as these cards are worth 10 points each in blackjack.
57+
const tenOfDiamonds = getCardValue("10♦");
58+
assertEquals(tenOfDiamonds, 10);
59+
60+
const jackOfClubs = getCardValue("J♣");
61+
assertEquals(jackOfClubs, 10);
4862

63+
const queenOfSpades = getCardValue("Q♠");
64+
assertEquals(queenOfSpades, 10);
65+
66+
const kingOfHearts = getCardValue("K♥");
67+
assertEquals(kingOfHearts, 10);
4968
// Handle Ace (A):
5069
// Given a card with a rank of "A",
5170
// When the function is called with an Ace,
@@ -55,3 +74,9 @@ const fiveofHearts = getCardValue("5♥");
5574
// Given a card with an invalid rank (neither a number nor a recognized face card),
5675
// When the function is called with such a card,
5776
// Then it should throw an error indicating "Invalid card rank."
77+
try {
78+
getCardValue("1♠");
79+
console.error("Test failed: invalid card did not throw error");
80+
} catch (error) {
81+
console.log("Invalid card test passed");
82+
}

0 commit comments

Comments
 (0)