Skip to content

Commit a6dd884

Browse files
committed
Implement getCardValue to return correct blackjack values for all valid cards and throw error for invalid ones
1 parent 85a7125 commit a6dd884

File tree

1 file changed

+24
-2
lines changed

1 file changed

+24
-2
lines changed

Sprint-3/1-key-implement/3-get-card-value.js

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +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") return 11;
11+
const rank = card.slice(0, -1); // remove the emoji
12+
if (rank === "A") return 11;
13+
if (["K", "Q", "J", "10"].includes(rank)) return 10;
14+
const number = Number(rank);
15+
if (number >= 2 && number <= 9) return number;
16+
throw new Error("Invalid card rank");
1217
}
1318

1419
// You need to write assertions for your function to check it works in different cases
@@ -33,19 +38,36 @@ assertEquals(aceofSpades, 11);
3338
// When the function is called with such a card,
3439
// Then it should return the numeric value corresponding to the rank (e.g., "5" should return 5).
3540
const fiveofHearts = getCardValue("5♥");
36-
// ====> write your test here, and then add a line to pass the test in the function above
41+
assertEquals(fiveofHearts, 5);
42+
3743

3844
// Handle Face Cards (J, Q, K):
3945
// Given a card with a rank of "10," "J," "Q," or "K",
4046
// When the function is called with such a card,
4147
// Then it should return the value 10, as these cards are worth 10 points each in blackjack.
48+
const jackofClubs = getCardValue("J♣");
49+
assertEquals(jackofClubs, 10);
50+
51+
const queenofDiamonds = getCardValue("Q♦");
52+
assertEquals(queenofDiamonds, 10);
53+
54+
const kingofHearts = getCardValue("K♥");
55+
assertEquals(kingofHearts, 10);
4256

4357
// Handle Ace (A):
4458
// Given a card with a rank of "A",
4559
// When the function is called with an Ace,
4660
// Then it should, by default, assume the Ace is worth 11 points, which is a common rule in blackjack.
4761

62+
const aceofSpades = getCardValue("A♠");
63+
assertEquals(aceofSpades, 11);
64+
4865
// Handle Invalid Cards:
4966
// Given a card with an invalid rank (neither a number nor a recognized face card),
5067
// When the function is called with such a card,
5168
// Then it should throw an error indicating "Invalid card rank."
69+
70+
71+
// getCardValue("Z♠"); // should throw "Invalid card rank"
72+
73+

0 commit comments

Comments
 (0)