Skip to content

Commit 6557f26

Browse files
committed
Implement getCardValue function with full tests, handling all card types (Ace, 2-10, J/Q/K), invalid card errors
1 parent e2c27cd commit 6557f26

File tree

1 file changed

+33
-3
lines changed

1 file changed

+33
-3
lines changed

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

Lines changed: 33 additions & 3 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+
const chars = [...card];
12+
const rank = chars.slice(0, -1).join('');
13+
1114
if (rank === "A") {
1215
return 11;
1316
}
17+
const parsed = Number(rank);
18+
if (!Number.isNaN(parsed) && parsed >= 2 && parsed <= 9) {
19+
return parsed;
20+
}
21+
if (rank === "10" || rank === "J" || rank === "Q" || rank === "K"){
22+
return 10;
23+
}
24+
throw new Error("invalid card rank.");
1425
}
1526

1627
// The line below allows us to load the getCardValue function into tests in other files.
@@ -31,20 +42,32 @@ function assertEquals(actualOutput, targetOutput) {
3142
// 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),
3243
// When the function getCardValue is called with this card string as input,
3344
// Then it should return the numerical card value
34-
const aceofSpades = getCardValue("A♠");
35-
assertEquals(aceofSpades, 11);
45+
const aceOfSpades = getCardValue("A♠");
46+
assertEquals(aceOfSpades, 11);
3647

3748
// Handle Number Cards (2-10):
3849
// Given a card with a rank between "2" and "9",
3950
// When the function is called with such a card,
4051
// Then it should return the numeric value corresponding to the rank (e.g., "5" should return 5).
41-
const fiveofHearts = getCardValue("5♥");
52+
const fiveOfHearts = getCardValue("5♥");
53+
assertEquals(fiveOfHearts, 5);
4254
// ====> write your test here, and then add a line to pass the test in the function above
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 tenofDiamonds = getCardValue("10♦");
61+
assertEquals(tenofDiamonds, 10);
62+
63+
const jackOfClubs = getCardValue("J♣");
64+
assertEquals(jackOfClubs, 10);
65+
66+
const queensOfSpades = getCardValue("Q♠")
67+
assertEquals(queensOfSpades, 10);
68+
69+
const kingOfHeart = getCardValue("K♥");
70+
assertEquals(kingOfHeart, 10);
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("1♠"); // invalid rank
83+
console.error("Test failed: invalid card did not throw error");
84+
} catch (error) {
85+
console.log("Invalid card test passed");
86+
}
87+

0 commit comments

Comments
 (0)