Skip to content

Commit 0d56af2

Browse files
committed
implement 3 get card value
1 parent c2f900d commit 0d56af2

File tree

1 file changed

+32
-2
lines changed

1 file changed

+32
-2
lines changed

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

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,19 @@
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") {
12-
return 11;
11+
const rank = card.slice(0, -1); //everything except the suit
12+
if (rank === "A") return 11;
13+
14+
if (["J", "Q", "K", "10"].includes(rank)) {
15+
return 10;
16+
}
17+
18+
const num = Number(rank);
19+
if (!isNaN(num) && num >= 2 && num <= 9) {
20+
return num;
1321
}
22+
throw new Error("Invalid card rank");
23+
1424
}
1525

1626
// The line below allows us to load the getCardValue function into tests in other files.
@@ -40,11 +50,24 @@ assertEquals(aceofSpades, 11);
4050
// Then it should return the numeric value corresponding to the rank (e.g., "5" should return 5).
4151
const fiveofHearts = getCardValue("5♥");
4252
// ====> write your test here, and then add a line to pass the test in the function above
53+
assertEquals(fiveofHearts, 5);
4354

4455
// Handle Face Cards (J, Q, K):
4556
// Given a card with a rank of "10," "J," "Q," or "K",
4657
// When the function is called with such a card,
4758
// Then it should return the value 10, as these cards are worth 10 points each in blackjack.
59+
const jack = getCardValue("J♣");
60+
assertEquals(jack, 10);
61+
62+
const queen = getCardValue("Q♦");
63+
assertEquals(queen, 10);
64+
65+
const king = getCardValue("K♥");
66+
assertEquals(king, 10);
67+
68+
const ten = getCardValue("10♠");
69+
assertEquals(ten, 10);
70+
4871

4972
// Handle Ace (A):
5073
// Given a card with a rank of "A",
@@ -55,3 +78,10 @@ const fiveofHearts = getCardValue("5♥");
5578
// Given a card with an invalid rank (neither a number nor a recognized face card),
5679
// When the function is called with such a card,
5780
// Then it should throw an error indicating "Invalid card rank."
81+
try {
82+
getCardValue("Z♠");
83+
console.assert(false, "Expected an error for invalid rank");
84+
} catch (e) {
85+
assertEquals(e.message, "Invalid card rank");
86+
}
87+

0 commit comments

Comments
 (0)