Skip to content

Commit 80d24b2

Browse files
committed
feat: implement getCardValue function with comprehensive test cases
1 parent 9d3f83b commit 80d24b2

File tree

1 file changed

+37
-7
lines changed

1 file changed

+37
-7
lines changed

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

Lines changed: 37 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,12 @@
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;
13-
}
11+
const rank = card.slice(0, -1);
12+
if (rank === "A") return 11;
13+
if (rank === "J" || rank === "Q" || rank === "K" || rank === "10") return 10;
14+
const numValue = parseInt(rank);
15+
if (numValue >= 2 && numValue <= 9) return numValue;
16+
throw new Error("Invalid card rank");
1417
}
1518

1619
// The line below allows us to load the getCardValue function into tests in other files.
@@ -31,27 +34,54 @@ function assertEquals(actualOutput, targetOutput) {
3134
// 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),
3235
// When the function getCardValue is called with this card string as input,
3336
// Then it should return the numerical card value
34-
const aceofSpades = getCardValue("A♠");
35-
assertEquals(aceofSpades, 11);
37+
38+
const aceOfSpades = getCardValue("A♠");
39+
assertEquals(aceOfSpades, 11);
3640

3741
// Handle Number Cards (2-10):
3842
// Given a card with a rank between "2" and "9",
3943
// When the function is called with such a card,
4044
// 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
45+
46+
const fiveOfHearts = getCardValue("5♥");
47+
assertEquals(fiveOfHearts, 5);
48+
const twoOfDiamonds = getCardValue("2♦");
49+
assertEquals(twoOfDiamonds, 2);
50+
const nineOfSpades = getCardValue("9♠");
51+
assertEquals(nineOfSpades, 9);
4352

4453
// Handle Face Cards (J, Q, K):
4554
// Given a card with a rank of "10," "J," "Q," or "K",
4655
// When the function is called with such a card,
4756
// Then it should return the value 10, as these cards are worth 10 points each in blackjack.
4857

58+
const kingOfHearts = getCardValue("K♥");
59+
assertEquals(kingOfHearts, 10);
60+
const queenOfSpades = getCardValue("Q♠");
61+
assertEquals(queenOfSpades, 10);
62+
const jackOfDiamonds = getCardValue("J♦");
63+
assertEquals(jackOfDiamonds, 10);
64+
const tenOfClubs = getCardValue("10♣");
65+
assertEquals(tenOfClubs, 10);
66+
4967
// Handle Ace (A):
5068
// Given a card with a rank of "A",
5169
// When the function is called with an Ace,
5270
// Then it should, by default, assume the Ace is worth 11 points, which is a common rule in blackjack.
5371

72+
const aceOfHearts = getCardValue("A♥");
73+
assertEquals(aceOfHearts, 11);
74+
5475
// Handle Invalid Cards:
5576
// Given a card with an invalid rank (neither a number nor a recognized face card),
5677
// When the function is called with such a card,
5778
// Then it should throw an error indicating "Invalid card rank."
79+
80+
try {
81+
getCardValue("X♠");
82+
console.assert(false, "Should have thrown an error for invalid card");
83+
} catch (error) {
84+
assertEquals(error.message, "Invalid card rank");
85+
}
86+
87+
console.log("✅ All tests passed!");

0 commit comments

Comments
 (0)