Skip to content

Commit f6e6f38

Browse files
committed
created testcases for get card value and tested with a pass
1 parent 61f0da7 commit f6e6f38

File tree

1 file changed

+34
-0
lines changed

1 file changed

+34
-0
lines changed

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

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,20 @@
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+
12+
const rank = card.slice(0, -1); // Extract rank before the suit
13+
1114
if (rank === "A") {
1215
return 11;
16+
} else if (["K", "Q", "J", "10"].includes(rank)) {
17+
return 10;
18+
} else if (!isNaN(rank) && Number(rank) >= 2 && Number(rank) <= 9) {
19+
return Number(rank);
20+
} else {
21+
throw new Error("Invalid card rank");
1322
}
23+
24+
1425
}
1526

1627
// The line below allows us to load the getCardValue function into tests in other files.
@@ -40,18 +51,41 @@ assertEquals(aceofSpades, 11);
4051
// Then it should return the numeric value corresponding to the rank (e.g., "5" should return 5).
4152
const fiveofHearts = getCardValue("5♥");
4253
// ====> write your test here, and then add a line to pass the test in the function above
54+
assertEquals(fiveofHearts, 5);
4355

4456
// Handle Face Cards (J, Q, K):
4557
// Given a card with a rank of "10," "J," "Q," or "K",
4658
// When the function is called with such a card,
4759
// Then it should return the value 10, as these cards are worth 10 points each in blackjack.
60+
const kingofDiamonds = getCardValue("K♦");
61+
assertEquals(kingofDiamonds, 10);
62+
const jackofClubs = getCardValue("J♣");
63+
assertEquals(jackofClubs, 10);
64+
const queenofHearts = getCardValue("Q♥");
65+
assertEquals(queenofHearts, 10);
66+
const tenofSpades = getCardValue("10♠");
67+
assertEquals(tenofSpades, 10);
4868

4969
// Handle Ace (A):
5070
// Given a card with a rank of "A",
5171
// When the function is called with an Ace,
5272
// Then it should, by default, assume the Ace is worth 11 points, which is a common rule in blackjack.
73+
const aceofClubs = getCardValue("A♣");
74+
assertEquals(aceofClubs, 11);
5375

5476
// Handle Invalid Cards:
5577
// Given a card with an invalid rank (neither a number nor a recognized face card),
5678
// When the function is called with such a card,
5779
// Then it should throw an error indicating "Invalid card rank."
80+
const invalidCard = () => getCardValue("1♠");
81+
// ====> write your test here, and then add a line to pass the test in the function above
82+
try {
83+
invalidCard();
84+
console.assert(false, "Expected an error to be thrown for invalid card rank");
85+
} catch (e) {
86+
console.assert(
87+
e.message === "Invalid card rank",
88+
`Expected error message to be "Invalid card rank" but got "${e.message}"`
89+
);
90+
}
91+

0 commit comments

Comments
 (0)