Skip to content

Commit ec1257e

Browse files
committed
Implement getCardValue function to handle card values and add tests for valid and invalid cards
1 parent 94d470e commit ec1257e

File tree

1 file changed

+29
-11
lines changed

1 file changed

+29
-11
lines changed
Lines changed: 29 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,17 @@
1-
// This problem involves playing cards: https://en.wikipedia.org/wiki/Standard_52-card_deck
2-
3-
// You will need to implement a function getCardValue
4-
// the function takes a single parameter, a string representing a playing card
5-
// the function should return the numerical value of the card
6-
// the first test and first case is written for you
7-
// complete the rest of the tests and cases
8-
// write one test at a time, and make it pass, build your solution up methodically
9-
// just make one change at a time -- don't rush -- programmers are deep and careful thinkers
101
function getCardValue(card) {
2+
const rank = card.slice(0, -1);
113
if (rank === "A") {
124
return 11;
135
}
6+
if (!isNaN(rank) && Number(rank) >= 2 && Number(rank) <= 10) {
7+
// checks for number cards 2-10
8+
return Number(rank);
9+
}
10+
if (["J", "Q", "K"].includes(rank)) {
11+
return 10;
12+
} else {
13+
throw new Error("Invalid card rank");
14+
}
1415
}
1516

1617
// The line below allows us to load the getCardValue function into tests in other files.
@@ -26,32 +27,49 @@ function assertEquals(actualOutput, targetOutput) {
2627
`Expected ${actualOutput} to equal ${targetOutput}`
2728
);
2829
}
29-
// Acceptance criteria:
3030

31+
// Case 1
3132
// 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),
3233
// When the function getCardValue is called with this card string as input,
3334
// Then it should return the numerical card value
3435
const aceofSpades = getCardValue("A♠");
3536
assertEquals(aceofSpades, 11);
3637

38+
// Case 2
3739
// Handle Number Cards (2-10):
3840
// Given a card with a rank between "2" and "9",
3941
// When the function is called with such a card,
4042
// Then it should return the numeric value corresponding to the rank (e.g., "5" should return 5).
4143
const fiveofHearts = getCardValue("5♥");
42-
// ====> write your test here, and then add a line to pass the test in the function above
44+
assertEquals(fiveofHearts, 5);
4345

46+
// Case 3
4447
// Handle Face Cards (J, Q, K):
4548
// Given a card with a rank of "10," "J," "Q," or "K",
4649
// When the function is called with such a card,
4750
// Then it should return the value 10, as these cards are worth 10 points each in blackjack.
51+
const kingofDiamonds = getCardValue("K♦");
52+
assertEquals(kingofDiamonds, 10);
4853

54+
// Case 4
4955
// Handle Ace (A):
5056
// Given a card with a rank of "A",
5157
// When the function is called with an Ace,
5258
// Then it should, by default, assume the Ace is worth 11 points, which is a common rule in blackjack.
59+
const aceofHearts = getCardValue("A♥");
60+
assertEquals(aceofHearts, 11);
5361

62+
// Case 5
5463
// Handle Invalid Cards:
5564
// Given a card with an invalid rank (neither a number nor a recognized face card),
5665
// When the function is called with such a card,
5766
// Then it should throw an error indicating "Invalid card rank."
67+
try {
68+
getCardValue("1♣");
69+
console.assert(false, "Expected error for invalid card");
70+
} catch (error) {
71+
console.assert(
72+
error.message === "Invalid card rank",
73+
`Expected "Invalid card rank" error, but got: ${error.message}`
74+
);
75+
}

0 commit comments

Comments
 (0)