Skip to content

Commit 20bc441

Browse files
committed
feat(sprint-3): implement getCardValue function
1 parent 519a564 commit 20bc441

File tree

1 file changed

+33
-37
lines changed

1 file changed

+33
-37
lines changed

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

Lines changed: 33 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -8,50 +8,46 @@
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

16-
// The line below allows us to load the getCardValue function into tests in other files.
17-
// This will be useful in the "rewrite tests with jest" step.
1819
module.exports = getCardValue;
1920

20-
// You need to write assertions for your function to check it works in different cases
21-
// we're going to use this helper function to make our assertions easier to read
22-
// if the actual output matches the target output, the test will pass
2321
function assertEquals(actualOutput, targetOutput) {
2422
console.assert(
2523
actualOutput === targetOutput,
2624
`Expected ${actualOutput} to equal ${targetOutput}`
2725
);
2826
}
29-
// Acceptance criteria:
30-
31-
// 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),
32-
// When the function getCardValue is called with this card string as input,
33-
// Then it should return the numerical card value
34-
const aceofSpades = getCardValue("A♠");
35-
assertEquals(aceofSpades, 11);
36-
37-
// Handle Number Cards (2-10):
38-
// Given a card with a rank between "2" and "9",
39-
// When the function is called with such a card,
40-
// 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
43-
44-
// Handle Face Cards (J, Q, K):
45-
// Given a card with a rank of "10," "J," "Q," or "K",
46-
// When the function is called with such a card,
47-
// Then it should return the value 10, as these cards are worth 10 points each in blackjack.
48-
49-
// Handle Ace (A):
50-
// Given a card with a rank of "A",
51-
// When the function is called with an Ace,
52-
// Then it should, by default, assume the Ace is worth 11 points, which is a common rule in blackjack.
53-
54-
// Handle Invalid Cards:
55-
// Given a card with an invalid rank (neither a number nor a recognized face card),
56-
// When the function is called with such a card,
57-
// Then it should throw an error indicating "Invalid card rank."
27+
28+
// Ace → 11
29+
assertEquals(getCardValue("A♠"), 11);
30+
31+
// Number Cards → numeric value
32+
assertEquals(getCardValue("5♥"), 5);
33+
assertEquals(getCardValue("2♦"), 2);
34+
assertEquals(getCardValue("9♣"), 9);
35+
36+
// Face Cards (J, Q, K, 10) → 10
37+
assertEquals(getCardValue("K♥"), 10);
38+
assertEquals(getCardValue("Q♠"), 10);
39+
assertEquals(getCardValue("J♦"), 10);
40+
assertEquals(getCardValue("10♣"), 10);
41+
42+
// Ace (all suits) → 11
43+
assertEquals(getCardValue("A♥"), 11);
44+
45+
// Invalid Card → throw error
46+
try {
47+
getCardValue("X♠");
48+
console.assert(false, "Should have thrown an error for invalid card");
49+
} catch (error) {
50+
assertEquals(error.message, "Invalid card rank");
51+
}
52+
53+
console.log("✅ All tests passed!");

0 commit comments

Comments
 (0)