Skip to content

Commit cea3512

Browse files
committed
Update: fix getCardValue function to handle face cards, numeric ranks, and invalid inputs with corresponding tests
1 parent e6d6e76 commit cea3512

File tree

1 file changed

+38
-1
lines changed

1 file changed

+38
-1
lines changed

Sprint-3/1-key-implement/3-get-card-value.js

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,22 @@
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-
if (rank === "A") return 11;
11+
const rank = card.slice(0, -1); // Extract the rank from the card string
12+
13+
if (rank === "A") return 11;
14+
15+
// Face cards and 10 are worth 10 points
16+
if (["10", "J", "Q", "K"].includes(rank)) {
17+
return 10;
18+
}
19+
// Convert rank to number and check if it's a valid number card
20+
const numericRank = Number(rank);
21+
if (!isNaN(numericRank)) {
22+
return numericRank;
23+
}
24+
// If rank is not valid, throw an error
25+
throw new Error("Invalid card rank.");
26+
// Note: The function assumes the input is always a valid card string
1227
}
1328

1429
// You need to write assertions for your function to check it works in different cases
@@ -34,18 +49,40 @@ assertEquals(aceofSpades, 11);
3449
// Then it should return the numeric value corresponding to the rank (e.g., "5" should return 5).
3550
const fiveofHearts = getCardValue("5♥");
3651
// ====> write your test here, and then add a line to pass the test in the function above
52+
assertEquals(fiveofHearts, 5); // 5 of Hearts should return 5 but still we need to implement this in the function
3753

3854
// Handle Face Cards (J, Q, K):
3955
// Given a card with a rank of "10," "J," "Q," or "K",
4056
// When the function is called with such a card,
4157
// Then it should return the value 10, as these cards are worth 10 points each in blackjack.
58+
const tenOfDiamonds = getCardValue("10♦");
59+
assertEquals(tenOfDiamonds, 10); // 10 of Diamonds should return 10
60+
const jackOfClubs = getCardValue("J♣");
61+
assertEquals(jackOfClubs, 10); // Jack of Clubs should return 10
62+
const queenOfSpades = getCardValue("Q♠");
63+
assertEquals(queenOfSpades, 10); // Queen of Spades should return 10
64+
const kingOfHearts = getCardValue("K♥");
65+
assertEquals(kingOfHearts, 10); // King of Hearts should return 10
4266

4367
// Handle Ace (A):
4468
// Given a card with a rank of "A",
4569
// When the function is called with an Ace,
4670
// Then it should, by default, assume the Ace is worth 11 points, which is a common rule in blackjack.
4771

72+
const aceOfDiamonds = getCardValue("A♦");
73+
assertEquals(aceOfDiamonds, 11); // Ace of Diamonds should return 11
74+
4875
// Handle Invalid Cards:
4976
// Given a card with an invalid rank (neither a number nor a recognized face card),
5077
// When the function is called with such a card,
5178
// Then it should throw an error indicating "Invalid card rank."
79+
// Invalid card test (should throw error)
80+
try {
81+
getCardValue("Z♠");
82+
console.assert(false, "Expected error for invalid rank");
83+
} catch (e) {
84+
console.assert(
85+
e.message === "Invalid card rank.",
86+
`Unexpected error message: ${e.message}`
87+
);
88+
}

0 commit comments

Comments
 (0)