Skip to content

Commit cce153a

Browse files
committed
complete 3-get-card-value
1 parent a3879b8 commit cce153a

File tree

1 file changed

+36
-4
lines changed

1 file changed

+36
-4
lines changed

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

Lines changed: 36 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,21 @@
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 rank = card.slice(0, -1);
12+
13+
if (/^[JQK]$/.test(rank)) {
14+
return 10;
15+
}
16+
1117
if (rank === "A") {
1218
return 11;
1319
}
20+
21+
if (/^[2-9]$/.test(rank) || rank === "10") {
22+
return Number(rank);
23+
}
24+
25+
throw new Error("Invalid card rank.");
1426
}
1527

1628
// The line below allows us to load the getCardValue function into tests in other files.
@@ -31,27 +43,47 @@ function assertEquals(actualOutput, targetOutput) {
3143
// 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),
3244
// When the function getCardValue is called with this card string as input,
3345
// Then it should return the numerical card value
34-
const aceofSpades = getCardValue("A♠");
35-
assertEquals(aceofSpades, 11);
46+
const aceOfSpades = getCardValue("A♠");
47+
assertEquals(aceOfSpades, 11);
3648

3749
// Handle Number Cards (2-10):
3850
// Given a card with a rank between "2" and "9",
3951
// When the function is called with such a card,
4052
// Then it should return the numeric value corresponding to the rank (e.g., "5" should return 5).
41-
const fiveofHearts = getCardValue("5♥");
42-
// ====> write your test here, and then add a line to pass the test in the function above
53+
const fiveOfHearts = getCardValue("5♥");
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 tenOfClubs = getCardValue("10♣");
61+
assertEquals(tenOfClubs, 10);
62+
63+
const jackOfDiamonds = getCardValue("J♦");
64+
assertEquals(jackOfDiamonds, 10);
65+
66+
const kingOfHearts = getCardValue("K♥");
67+
assertEquals(kingOfHearts, 10);
68+
69+
const queenOfSpades = getCardValue("Q♠");
70+
assertEquals(queenOfSpades, 10);
4871

4972
// Handle Ace (A):
5073
// Given a card with a rank of "A",
5174
// When the function is called with an Ace,
5275
// Then it should, by default, assume the Ace is worth 11 points, which is a common rule in blackjack.
76+
const aceOfDiamonds = getCardValue("A♦");
77+
assertEquals(aceOfDiamonds, 11);
5378

5479
// Handle Invalid Cards:
5580
// Given a card with an invalid rank (neither a number nor a recognized face card),
5681
// When the function is called with such a card,
5782
// Then it should throw an error indicating "Invalid card rank."
83+
let errorMessage = "";
84+
try {
85+
getCardValue("A");
86+
} catch (error) {
87+
errorMessage = error.message;
88+
}
89+
assertEquals(errorMessage, "Invalid card rank.");

0 commit comments

Comments
 (0)