Skip to content

Commit cfda58c

Browse files
committed
Update getCardValue function to return correct numerical values and throw an error for invalid cards
1 parent f9eea26 commit cfda58c

File tree

1 file changed

+27
-2
lines changed

1 file changed

+27
-2
lines changed

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

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,17 @@
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") {
11+
card = card.substring(0 , card.length -1); // Remove the suit emoji
12+
console.log(card);
13+
if (card === "A") {
1214
return 11;
15+
} if (card === "J" || card === "Q" || card === "K" || card === "10") {
16+
return 10;
17+
} else if (card >= 2 && card <=9) {
18+
card = Number(card);
19+
return card;
20+
} else {
21+
throw new Error("Invalid card rank.");
1322
}
1423
}
1524

@@ -28,7 +37,8 @@ function assertEquals(actualOutput, targetOutput) {
2837
}
2938
// Acceptance criteria:
3039

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),
40+
// Given a card string in the format "A♠" (representing a card in blackjack - the last character will always be an emoji for a suit,
41+
// and all characters before will be a number 2-10, or one letter of J, Q, K, A),
3242
// When the function getCardValue is called with this card string as input,
3343
// Then it should return the numerical card value
3444
const aceofSpades = getCardValue("A♠");
@@ -40,11 +50,23 @@ assertEquals(aceofSpades, 11);
4050
// Then it should return the numeric value corresponding to the rank (e.g., "5" should return 5).
4151
const fiveofHearts = getCardValue("5♥");
4252
// ====> write your test here, and then add a line to pass the test in the function above
53+
assertEquals(fiveofHearts, 5);
4354

4455
// Handle Face Cards (J, Q, K):
4556
// Given a card with a rank of "10," "J," "Q," or "K",
4657
// When the function is called with such a card,
4758
// Then it should return the value 10, as these cards are worth 10 points each in blackjack.
59+
const jackOfDiamonds = getCardValue("J♦");
60+
assertEquals(jackOfDiamonds, 10);
61+
62+
const queenOfClubs = getCardValue("Q♣");
63+
assertEquals(queenOfClubs, 10);
64+
65+
const kingOfHearts = getCardValue("K♥");
66+
assertEquals(kingOfHearts, 10);
67+
68+
const tenOfSpades = getCardValue("10♠");
69+
assertEquals(tenOfSpades, 10);
4870

4971
// Handle Ace (A):
5072
// Given a card with a rank of "A",
@@ -55,3 +77,6 @@ const fiveofHearts = getCardValue("5♥");
5577
// Given a card with an invalid rank (neither a number nor a recognized face card),
5678
// When the function is called with such a card,
5779
// Then it should throw an error indicating "Invalid card rank."
80+
81+
82+
const invalidRandomCard = getCardValue("15♠");

0 commit comments

Comments
 (0)