Skip to content

Commit dfebb97

Browse files
committed
added conditions for all ranks in a deck of cards and tests for them
1 parent 7daf16b commit dfebb97

File tree

1 file changed

+19
-4
lines changed

1 file changed

+19
-4
lines changed

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

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,18 @@
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") {
11+
const rank = card[0];
12+
13+
if (card === "A♠") {
14+
return 11;
15+
} else if (Number(rank) > 1 && Number(rank) < 10) {
16+
return Number(rank);
17+
} else if (rank === "10" || rank === "J" || rank === "Q" || rank === "K") {
18+
return 10;
19+
} else if (rank === "A") {
1220
return 11;
21+
} else {
22+
return "Invalid card rank.";
1323
}
1424
}
1525

@@ -38,20 +48,25 @@ assertEquals(aceofSpades, 11);
3848
// Given a card with a rank between "2" and "9",
3949
// When the function is called with such a card,
4050
// Then it should return the numeric value corresponding to the rank (e.g., "5" should return 5).
41-
const fiveofHearts = getCardValue("5♥");
51+
const fiveofHearts = getCardValue("9♥");
4252
// ====> write your test here, and then add a line to pass the test in the function above
43-
53+
assertEquals(fiveofHearts, 9);
4454
// Handle Face Cards (J, Q, K):
4555
// Given a card with a rank of "10," "J," "Q," or "K",
4656
// When the function is called with such a card,
4757
// Then it should return the value 10, as these cards are worth 10 points each in blackjack.
48-
58+
const faceCards = getCardValue("J♥");
59+
assertEquals(faceCards, 10);
4960
// Handle Ace (A):
5061
// Given a card with a rank of "A",
5162
// When the function is called with an Ace,
5263
// Then it should, by default, assume the Ace is worth 11 points, which is a common rule in blackjack.
64+
const ace = getCardValue("A♥");
65+
assertEquals(ace, 11);
5366

5467
// Handle Invalid Cards:
5568
// Given a card with an invalid rank (neither a number nor a recognized face card),
5669
// When the function is called with such a card,
5770
// Then it should throw an error indicating "Invalid card rank."
71+
const invalidCard = getCardValue("15♠");
72+
assertEquals(invalidCard, "Invalid card rank.");

0 commit comments

Comments
 (0)