Skip to content

Commit 121ebe4

Browse files
committed
Add test cases and update getCardValue to handle all card ranks
1 parent 31446ec commit 121ebe4

File tree

1 file changed

+34
-22
lines changed

1 file changed

+34
-22
lines changed

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

Lines changed: 34 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -8,17 +8,29 @@
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-
card = card.substring(0 , card.length -1); // Remove the suit emoji
12-
if (card === "A") {
13-
return 11;
14-
} if (card === "J" || card === "Q" || card === "K" || card === "10") {
15-
return 10;
16-
} else if (card >= 2 && card <=9) {
17-
card = Number(card);
18-
return card;
11+
let rank = card.substring(0, card.length - 1); // Remove the suit emoji
12+
let actualOutput;
13+
14+
if (rank === "A") {
15+
actualOutput = 11;
16+
} else if (rank === "J" || rank === "Q" || rank === "K" || rank === "10") {
17+
actualOutput = 10;
18+
} else if (
19+
rank === "2" ||
20+
rank === "3" ||
21+
rank === "4" ||
22+
rank === "5" ||
23+
rank === "6" ||
24+
rank === "7" ||
25+
rank === "8" ||
26+
rank === "9"
27+
) {
28+
actualOutput = Number(rank);
1929
} else {
20-
return "Invalid card rank.";
30+
actualOutput = "Invalid card rank.";
2131
}
32+
33+
return actualOutput;
2234
}
2335

2436
// The line below allows us to load the getCardValue function into tests in other files.
@@ -36,20 +48,20 @@ function assertEquals(actualOutput, targetOutput) {
3648
}
3749
// Acceptance criteria:
3850

39-
// Given a card string in the format "A♠" (representing a card in blackjack - the last character will always be an emoji for a suit,
51+
// Given a card string in the format "A♠" (representing a card in blackjack - the last character will always be an emoji for a suit,
4052
// and all characters before will be a number 2-10, or one letter of J, Q, K, A),
4153
// When the function getCardValue is called with this card string as input,
4254
// Then it should return the numerical card value
43-
const aceofSpades = getCardValue("A♠");
44-
assertEquals(aceofSpades, 11);
55+
const aceOfSpades = getCardValue("A♠");
56+
assertEquals(aceOfSpades, 11);
4557

4658
// Handle Number Cards (2-10):
4759
// Given a card with a rank between "2" and "9",
4860
// When the function is called with such a card,
4961
// Then it should return the numeric value corresponding to the rank (e.g., "5" should return 5).
50-
const fiveofHearts = getCardValue("5♥");
62+
const fiveOfHearts = getCardValue("5♥");
5163
// ====> write your test here, and then add a line to pass the test in the function above
52-
assertEquals(fiveofHearts, 5);
64+
assertEquals(fiveOfHearts, 5);
5365

5466
// Handle Face Cards (J, Q, K):
5567
// Given a card with a rank of "10," "J," "Q," or "K",
@@ -65,18 +77,18 @@ const kingOfHearts = getCardValue("K♥");
6577
assertEquals(kingOfHearts, 10);
6678

6779
const tenOfSpades = getCardValue("10♠");
68-
assertEquals(tenOfSpades, 10);
69-
70-
// Handle Ace (A):
71-
// Given a card with a rank of "A",
72-
// When the function is called with an Ace,
73-
// Then it should, by default, assume the Ace is worth 11 points, which is a common rule in blackjack.
80+
assertEquals(tenOfSpades, 10);
7481

7582
// Handle Invalid Cards:
7683
// Given a card with an invalid rank (neither a number nor a recognized face card),
7784
// When the function is called with such a card,
7885
// Then it should throw an error indicating "Invalid card rank."
86+
const invalidRandomCard = getCardValue("15♠");
87+
assertEquals(invalidRandomCard, "Invalid card rank.");
7988

89+
const oneOfSpades = getCardValue("1♠");
90+
assertEquals(oneOfSpades, "Invalid card rank.");
91+
92+
const emptyCard = getCardValue("");
93+
assertEquals(emptyCard, "Invalid card rank.");
8094

81-
const invalidRandomCard = getCardValue("15♠");
82-
//assertEquals(invalidRandomCard, "Invalid card rank.");

0 commit comments

Comments
 (0)