Skip to content

Commit b475ea9

Browse files
committed
Implement getCardValue function to handle card values and add tests for A, face cards, number cards, and invalid inputs
1 parent 954149a commit b475ea9

File tree

1 file changed

+29
-3
lines changed

1 file changed

+29
-3
lines changed

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

Lines changed: 29 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,23 @@
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); // <-- answer
12+
1113
if (rank === "A") {
12-
return 11;
14+
return 11; // <-- answer
15+
}
16+
17+
if (["K", "Q", "J", "10"].includes(rank)) {
18+
return 10; // <-- answer
1319
}
20+
21+
const num = Number(rank); // <-- answer
22+
23+
if (num >= 2 && num <= 9) {
24+
return num; // <-- answer
25+
}
26+
27+
throw new Error("Invalid card rank"); // <-- answer
1428
}
1529

1630
// The line below allows us to load the getCardValue function into tests in other files.
@@ -26,32 +40,44 @@ function assertEquals(actualOutput, targetOutput) {
2640
`Expected ${actualOutput} to equal ${targetOutput}`
2741
);
2842
}
43+
2944
// Acceptance criteria:
3045

3146
// 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),
3247
// When the function getCardValue is called with this card string as input,
3348
// Then it should return the numerical card value
3449
const aceofSpades = getCardValue("A♠");
35-
assertEquals(aceofSpades, 11);
50+
assertEquals(aceofSpades, 11); // <-- answer
3651

3752
// Handle Number Cards (2-10):
3853
// Given a card with a rank between "2" and "9",
3954
// When the function is called with such a card,
4055
// Then it should return the numeric value corresponding to the rank (e.g., "5" should return 5).
4156
const fiveofHearts = getCardValue("5♥");
42-
// ====> write your test here, and then add a line to pass the test in the function above
57+
assertEquals(fiveofHearts, 5); // <-- answer
4358

4459
// Handle Face Cards (J, Q, K):
4560
// Given a card with a rank of "10," "J," "Q," or "K",
4661
// When the function is called with such a card,
4762
// Then it should return the value 10, as these cards are worth 10 points each in blackjack.
63+
assertEquals(getCardValue("10♦"), 10); // <-- answer
64+
assertEquals(getCardValue("J♣"), 10); // <-- answer
65+
assertEquals(getCardValue("Q♠"), 10); // <-- answer
66+
assertEquals(getCardValue("K♥"), 10); // <-- answer
4867

4968
// Handle Ace (A):
5069
// Given a card with a rank of "A",
5170
// When the function is called with an Ace,
5271
// Then it should, by default, assume the Ace is worth 11 points, which is a common rule in blackjack.
72+
assertEquals(getCardValue("A♦"), 11); // <-- answer
5373

5474
// Handle Invalid Cards:
5575
// Given a card with an invalid rank (neither a number nor a recognized face card),
5676
// When the function is called with such a card,
5777
// Then it should throw an error indicating "Invalid card rank."
78+
try {
79+
getCardValue("Z♠");
80+
console.assert(false, "Expected an error"); // <-- answer
81+
} catch (e) {
82+
console.assert(e.message === "Invalid card rank"); // <-- answer
83+
}

0 commit comments

Comments
 (0)