Skip to content

Commit 2462c94

Browse files
committed
Solved task 1-implement/3-get-card-value.js
1 parent 0eef99b commit 2462c94

File tree

1 file changed

+68
-2
lines changed

1 file changed

+68
-2
lines changed

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

Lines changed: 68 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,47 @@
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+
const rank = card.slice(0, -1);
12+
const cardFace = card.slice(-1);
13+
const numRank = Number(rank);
14+
15+
if (
16+
rank === "A" &&
17+
(cardFace === "♠" ||
18+
cardFace === "♣" ||
19+
cardFace === "♦" ||
20+
cardFace === "♥")
21+
) {
1222
return 11;
23+
} else {
24+
("Invalid card rank.");
25+
}
26+
27+
if (
28+
(rank === "10" || rank === "J" || rank === "Q" || rank === "K") &&
29+
(cardFace === "♠" ||
30+
cardFace === "♣" ||
31+
cardFace === "♦" ||
32+
cardFace === "♥")
33+
) {
34+
return 10;
35+
} else {
36+
return "Invalid card rank.";
37+
}
38+
39+
if (
40+
Number.isInteger(numRank) &&
41+
numRank >= 2 &&
42+
numRank <= 9 &&
43+
(cardFace === "♠" ||
44+
cardFace === "♣" ||
45+
cardFace === "♦" ||
46+
cardFace === "♥")
47+
) {
48+
console.log("numRank is", rank);
49+
return numRank;
50+
} else {
51+
return "Invalid card rank.";
1352
}
1453
}
1554

@@ -39,19 +78,46 @@ assertEquals(aceofSpades, 11);
3978
// When the function is called with such a card,
4079
// Then it should return the numeric value corresponding to the rank (e.g., "5" should return 5).
4180
const fiveofHearts = getCardValue("5♥");
42-
// ====> write your test here, and then add a line to pass the test in the function above
81+
assertEquals(fiveofHearts, 5);
4382

4483
// Handle Face Cards (J, Q, K):
4584
// Given a card with a rank of "10," "J," "Q," or "K",
4685
// When the function is called with such a card,
4786
// Then it should return the value 10, as these cards are worth 10 points each in blackjack.
87+
const tenOfDiamonds = getCardValue("10♦");
88+
assertEquals(tenOfDiamonds, 10);
89+
const jackOfClubs = getCardValue("J♣");
90+
assertEquals(jackOfClubs, 10);
91+
const queenOfSpades = getCardValue("Q♠");
92+
assertEquals(queenOfSpades, 10);
93+
const kingOfHearts = getCardValue("K♥");
94+
assertEquals(kingOfHearts, 10);
4895

4996
// Handle Ace (A):
5097
// Given a card with a rank of "A",
5198
// When the function is called with an Ace,
5299
// Then it should, by default, assume the Ace is worth 11 points, which is a common rule in blackjack.
100+
const aceOfDiamonds = getCardValue("A♦");
101+
assertEquals(aceOfDiamonds, 11);
102+
const aceOfClubs = getCardValue("A♣");
103+
assertEquals(aceOfClubs, 11);
104+
const aceOfHearts = getCardValue("A♥");
105+
assertEquals(aceOfHearts, 11);
106+
//Actually, these cases already covered in the function getCardValue()
53107

54108
// Handle Invalid Cards:
55109
// Given a card with an invalid rank (neither a number nor a recognized face card),
56110
// When the function is called with such a card,
57111
// Then it should throw an error indicating "Invalid card rank."
112+
const invalidNumberCard = getCardValue("12♥");
113+
assertEquals(invalidNumberCard, "Invalid card rank.");
114+
const invalidFaceCard = getCardValue("5*");
115+
assertEquals(invalidFaceCard, "Invalid card rank.");
116+
const invalidFaceCard10 = getCardValue("10$");
117+
assertEquals(invalidFaceCard10, "Invalid card rank.");
118+
const invalidFaceCardJack = getCardValue("J%");
119+
assertEquals(invalidFaceCardJack, "Invalid card rank.");
120+
const invalidFaceCardQueen = getCardValue("Q£");
121+
assertEquals(invalidFaceCardQueen, "Invalid card rank.");
122+
const invalidFaceCardKing = getCardValue("K^");
123+
assertEquals(invalidFaceCardKing, "Invalid card rank.");

0 commit comments

Comments
 (0)