Skip to content

Commit a08e5c7

Browse files
committed
Completed 3-get-card-value.js exercise
1 parent 2b5822f commit a08e5c7

File tree

1 file changed

+14
-0
lines changed

1 file changed

+14
-0
lines changed

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

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,15 @@
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, card.length - 1);
1112
if (rank === "A") {
1213
return 11;
14+
} else if (Number(rank) >= 2 && Number(rank) < 10) {
15+
return Number(rank);
16+
} else if (rank === "10" || rank === "J" || rank === "Q" || rank === "K") {
17+
return 10;
18+
} else {
19+
return "Invalid card rank";
1320
}
1421
}
1522

@@ -40,18 +47,25 @@ assertEquals(aceofSpades, 11);
4047
// Then it should return the numeric value corresponding to the rank (e.g., "5" should return 5).
4148
const fiveofHearts = getCardValue("5♥");
4249
// ====> write your test here, and then add a line to pass the test in the function above
50+
assertEquals(fiveofHearts, 5);
4351

4452
// Handle Face Cards (J, Q, K):
4553
// Given a card with a rank of "10," "J," "Q," or "K",
4654
// When the function is called with such a card,
4755
// Then it should return the value 10, as these cards are worth 10 points each in blackjack.
56+
const kingofDiamonds = getCardValue("K♦");
57+
assertEquals(kingofDiamonds, 10);
4858

4959
// Handle Ace (A):
5060
// Given a card with a rank of "A",
5161
// When the function is called with an Ace,
5262
// Then it should, by default, assume the Ace is worth 11 points, which is a common rule in blackjack.
63+
const aceofClubs = getCardValue("A♣");
64+
assertEquals(aceofClubs, 11);
5365

5466
// Handle Invalid Cards:
5567
// Given a card with an invalid rank (neither a number nor a recognized face card),
5668
// When the function is called with such a card,
5769
// Then it should throw an error indicating "Invalid card rank."
70+
const invalidCard = getCardValue("1♠");
71+
assertEquals(invalidCard, "Invalid card rank");

0 commit comments

Comments
 (0)