Skip to content

Commit 9ddec99

Browse files
committed
Refactor getCardValue function to streamline rank handling and improve test organization
1 parent ccd4e00 commit 9ddec99

File tree

1 file changed

+35
-46
lines changed

1 file changed

+35
-46
lines changed

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

Lines changed: 35 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -7,27 +7,33 @@
77
// complete the rest of the tests and cases
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
10+
1011
function getCardValue(card) {
11-
// Extract the rank (everything except the last character, which is the suit emoji)
12+
// Extract rank (all characters except final suit emoji)
1213
const rank = card.slice(0, -1);
1314

14-
// Handle Ace
15-
if (rank === "A") {
16-
return 11;
15+
// Mapping for face card values
16+
const faceValues = {
17+
J: 10,
18+
Q: 10,
19+
K: 10,
20+
A: 11,
21+
};
22+
23+
// If rank is a face card, return mapped value
24+
if (faceValues[rank]) {
25+
return faceValues[rank];
1726
}
1827

19-
// Handle Face Cards (J, Q, K)
20-
if (rank === "J" || rank === "Q" || rank === "K" || rank === "10") {
21-
return 10;
22-
}
28+
// Valid number ranks 2–10 only
29+
const validNumberRanks = ["2", "3", "4", "5", "6", "7", "8", "9", "10"];
2330

24-
// Handle Number Cards (2-9)
25-
if (!isNaN(rank)) {
31+
if (validNumberRanks.includes(rank)) {
2632
return Number(rank);
2733
}
2834

29-
// Handle Invalid Cards
30-
throw new Error("Invalid card rank.");
35+
// Anything else = invalid
36+
throw new Error("Invalid card rank");
3137
}
3238

3339
// The line below allows us to load the getCardValue function into tests in other files.
@@ -44,45 +50,28 @@ function assertEquals(actualOutput, targetOutput) {
4450
);
4551
}
4652

47-
// Acceptance criteria:
48-
49-
// 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),
50-
// When the function getCardValue is called with this card string as input,
51-
// Then it should return the numerical card value
52-
const aceofSpades = getCardValue("A♠");
53-
assertEquals(aceofSpades, 11);
53+
// ---------------- TESTS ----------------
5454

55-
// Handle Number Cards (2-10):
56-
// Given a card with a rank between "2" and "9",
57-
// When the function is called with such a card,
58-
// Then it should return the numeric value corresponding to the rank (e.g., "5" should return 5).
59-
const fiveofHearts = getCardValue("5♥");
60-
assertEquals(fiveofHearts, 5);
55+
// Handle Ace (A)
56+
const aceOfSpades = getCardValue("A♠");
57+
assertEquals(aceOfSpades, 11);
6158

62-
const tenofClubs = getCardValue("10♣");
63-
assertEquals(tenofClubs, 10);
59+
// Handle Number Cards (2-9)
60+
const fiveOfHearts = getCardValue("5♥");
61+
assertEquals(fiveOfHearts, 5);
6462

65-
// Handle Face Cards (J, Q, K):
66-
// Given a card with a rank of "J," "Q," or "K",
67-
// When the function is called with such a card,
68-
// Then it should return the value 10, as these cards are worth 10 points each in blackjack.
69-
const jackOfDiamonds = getCardValue("J♦");
70-
assertEquals(jackOfDiamonds, 10);
63+
// Handle 10
64+
const tenOfClubs = getCardValue("10♣");
65+
assertEquals(tenOfClubs, 10);
7166

72-
const queenOfSpades = getCardValue("Q♠");
73-
assertEquals(queenOfSpades, 10);
67+
// Handle Face Cards (J, Q, K)
68+
const queenOfDiamonds = getCardValue("Q♦");
69+
assertEquals(queenOfDiamonds, 10);
7470

75-
const kingOfHearts = getCardValue("K♥");
76-
assertEquals(kingOfHearts, 10);
77-
78-
// Handle Invalid Cards:
79-
// Given a card with an invalid rank (neither a number nor a recognized face card),
80-
// When the function is called with such a card,
81-
// Then it should throw an error indicating "Invalid card rank."
71+
// Handle invalid card
8272
try {
83-
getCardValue("Z♠");
73+
getCardValue("15♣"); // invalid rank
74+
console.assert(false, "Expected error for invalid rank");
8475
} catch (e) {
85-
assertEquals(e.message, "Invalid card rank.");
76+
console.assert(true);
8677
}
87-
88-
console.log("All tests ran! If no errors, all passed.");

0 commit comments

Comments
 (0)