Skip to content

Commit 2e73bdc

Browse files
implemented card value logic and added tests for face cards and error handling
1 parent 996ebed commit 2e73bdc

File tree

1 file changed

+35
-5
lines changed

1 file changed

+35
-5
lines changed

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

Lines changed: 35 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,24 @@
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+
let rank = card.slice(0, -1);
12+
let cardFace = card[card.length - 1];
13+
14+
if (!["♠", "♥", "♦", "♣"].includes(cardFace)) {
15+
throw new Error(`Invalid card face: ${cardFace}`);
16+
}
1117
if (rank === "A") {
1218
return 11;
1319
}
20+
if (+rank >= 2 && +rank <= 9) {
21+
return +rank;
22+
}
23+
if (["K", "10", "Q", "J"].includes(rank)) {
24+
return 10;
25+
}
26+
if (!["♠", "♥", "♦", "♣"].includes(cardFace)) {
27+
}
28+
throw new Error(`Invalid card rank: ${rank}`);
1429
}
1530

1631
// The line below allows us to load the getCardValue function into tests in other files.
@@ -26,26 +41,28 @@ function assertEquals(actualOutput, targetOutput) {
2641
`Expected ${actualOutput} to equal ${targetOutput}`
2742
);
2843
}
44+
2945
// Acceptance criteria:
3046

3147
// 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),
3248
// When the function getCardValue is called with this card string as input,
3349
// Then it should return the numerical card value
34-
const aceofSpades = getCardValue("A♠");
35-
assertEquals(aceofSpades, 11);
50+
const aceOfSpades = getCardValue("A♠");
51+
assertEquals(aceOfSpades, 11);
3652

3753
// Handle Number Cards (2-10):
3854
// Given a card with a rank between "2" and "9",
3955
// When the function is called with such a card,
4056
// 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
57+
const fiveOfHearts = getCardValue("5♥");
58+
assertEquals(fiveOfHearts, 5);
4359

4460
// Handle Face Cards (J, Q, K):
4561
// Given a card with a rank of "10," "J," "Q," or "K",
4662
// When the function is called with such a card,
4763
// Then it should return the value 10, as these cards are worth 10 points each in blackjack.
48-
64+
const kingOfHearts = getCardValue("K♥");
65+
assertEquals(kingOfHearts, 10);
4966
// Handle Ace (A):
5067
// Given a card with a rank of "A",
5168
// When the function is called with an Ace,
@@ -55,3 +72,16 @@ const fiveofHearts = getCardValue("5♥");
5572
// Given a card with an invalid rank (neither a number nor a recognized face card),
5673
// When the function is called with such a card,
5774
// Then it should throw an error indicating "Invalid card rank."
75+
try {
76+
getCardValue("2");
77+
throw new Error("Should have thrown an error for invalid card");
78+
} catch (error) {
79+
assertEquals(error.message, "Invalid card face: 2");
80+
}
81+
82+
try {
83+
getCardValue("1♥");
84+
throw new Error("Should have thrown an error for invalid card");
85+
} catch (error) {
86+
assertEquals(error.message, "Invalid card rank: 1");
87+
}

0 commit comments

Comments
 (0)