Skip to content

Commit 5d8d05c

Browse files
committed
get card value sorted.
1 parent 0de42f1 commit 5d8d05c

File tree

1 file changed

+22
-1
lines changed

1 file changed

+22
-1
lines changed

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

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,11 @@
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);
1112
if (rank === "A") return 11;
13+
if (rank === "J" || rank === "Q" || rank === "K" || rank === "10") return 10;
14+
if (!isNaN(rank)) return Number(rank);
15+
throw new Error("Invalid card rank.");
1216
}
1317

1418
// You need to write assertions for your function to check it works in different cases
@@ -33,12 +37,23 @@ assertEquals(aceofSpades, 11);
3337
// When the function is called with such a card,
3438
// Then it should return the numeric value corresponding to the rank (e.g., "5" should return 5).
3539
const fiveofHearts = getCardValue("5♥");
36-
// ====> write your test here, and then add a line to pass the test in the function above
40+
assertEquals(fiveofHearts, 5);
3741

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

4358
// Handle Ace (A):
4459
// Given a card with a rank of "A",
@@ -49,3 +64,9 @@ const fiveofHearts = getCardValue("5♥");
4964
// Given a card with an invalid rank (neither a number nor a recognized face card),
5065
// When the function is called with such a card,
5166
// Then it should throw an error indicating "Invalid card rank."
67+
try {
68+
getCardValue("Z♠");
69+
console.error("Error was expected but not thrown");
70+
} catch (e) {
71+
assertEquals(e.message, "Invalid card rank.");
72+
}

0 commit comments

Comments
 (0)