Skip to content

Commit 6032ba9

Browse files
committed
Implement getCardValue function to handle card ranks, including Aces, face cards, and invalid inputs
1 parent 878d49e commit 6032ba9

File tree

1 file changed

+37
-6
lines changed

1 file changed

+37
-6
lines changed

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

Lines changed: 37 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,26 @@
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+
// Extract the rank (everything except the last character, which is the suit emoji)
12+
const rank = card.slice(0, -1);
13+
14+
// Handle Ace
1115
if (rank === "A") {
1216
return 11;
1317
}
18+
19+
// Handle Face Cards (J, Q, K)
20+
if (rank === "J" || rank === "Q" || rank === "K" || rank === "10") {
21+
return 10;
22+
}
23+
24+
// Handle Number Cards (2-9)
25+
if (!isNaN(rank)) {
26+
return Number(rank);
27+
}
28+
29+
// Handle Invalid Cards
30+
throw new Error("Invalid card rank.");
1431
}
1532

1633
// The line below allows us to load the getCardValue function into tests in other files.
@@ -26,6 +43,7 @@ function assertEquals(actualOutput, targetOutput) {
2643
`Expected ${actualOutput} to equal ${targetOutput}`
2744
);
2845
}
46+
2947
// Acceptance criteria:
3048

3149
// 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),
@@ -39,19 +57,32 @@ assertEquals(aceofSpades, 11);
3957
// When the function is called with such a card,
4058
// Then it should return the numeric value corresponding to the rank (e.g., "5" should return 5).
4159
const fiveofHearts = getCardValue("5♥");
42-
// ====> write your test here, and then add a line to pass the test in the function above
60+
assertEquals(fiveofHearts, 5);
61+
62+
const tenofClubs = getCardValue("10♣");
63+
assertEquals(tenofClubs, 10);
4364

4465
// Handle Face Cards (J, Q, K):
45-
// Given a card with a rank of "10," "J," "Q," or "K",
66+
// Given a card with a rank of "J," "Q," or "K",
4667
// When the function is called with such a card,
4768
// Then it should return the value 10, as these cards are worth 10 points each in blackjack.
69+
const jackOfDiamonds = getCardValue("J♦");
70+
assertEquals(jackOfDiamonds, 10);
71+
72+
const queenOfSpades = getCardValue("Q♠");
73+
assertEquals(queenOfSpades, 10);
4874

49-
// Handle Ace (A):
50-
// Given a card with a rank of "A",
51-
// When the function is called with an Ace,
52-
// Then it should, by default, assume the Ace is worth 11 points, which is a common rule in blackjack.
75+
const kingOfHearts = getCardValue("K♥");
76+
assertEquals(kingOfHearts, 10);
5377

5478
// Handle Invalid Cards:
5579
// Given a card with an invalid rank (neither a number nor a recognized face card),
5680
// When the function is called with such a card,
5781
// Then it should throw an error indicating "Invalid card rank."
82+
try {
83+
getCardValue("Z♠");
84+
} catch (e) {
85+
assertEquals(e.message, "Invalid card rank.");
86+
}
87+
88+
console.log("All tests ran! If no errors, all passed.");

0 commit comments

Comments
 (0)