Skip to content

Commit bc1b331

Browse files
committed
adding tests for card values in blackjack
1 parent bc00dfc commit bc1b331

File tree

2 files changed

+75
-5
lines changed

2 files changed

+75
-5
lines changed

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

Lines changed: 29 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,18 @@
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+
const rank = card.slice(0, -1);
1112
if (rank === "A") {
1213
return 11;
1314
}
15+
if (rank === "K" || rank === "Q" || rank === "J" || rank === "10") {
16+
return 10;
17+
}
18+
const numericRank = Number(rank);
19+
if (numericRank >= 2 && numericRank <= 9) {
20+
return numericRank;
21+
}
22+
return "Error: Invalid card rank";
1423
}
1524

1625
// The line below allows us to load the getCardValue function into tests in other files.
@@ -31,27 +40,44 @@ function assertEquals(actualOutput, targetOutput) {
3140
// 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),
3241
// When the function getCardValue is called with this card string as input,
3342
// Then it should return the numerical card value
34-
const aceofSpades = getCardValue("A♠");
35-
assertEquals(aceofSpades, 11);
43+
const aceOfSpades = getCardValue("A♠");
44+
assertEquals(aceOfSpades, 11);
3645

3746
// Handle Number Cards (2-10):
3847
// Given a card with a rank between "2" and "9",
3948
// When the function is called with such a card,
4049
// Then it should return the numeric value corresponding to the rank (e.g., "5" should return 5).
41-
const fiveofHearts = getCardValue("5♥");
50+
const nineOfHearts = getCardValue("9♥");
4251
// ====> write your test here, and then add a line to pass the test in the function above
52+
assertEquals(nineOfHearts, 9);
4353

4454
// Handle Face Cards (J, Q, K):
4555
// Given a card with a rank of "10," "J," "Q," or "K",
4656
// When the function is called with such a card,
4757
// Then it should return the value 10, as these cards are worth 10 points each in blackjack.
58+
const kingOfDiamonds = getCardValue("K♦");
59+
assertEquals(kingOfDiamonds, 10);
60+
const queenOfClubs = getCardValue("Q♣");
61+
assertEquals(queenOfClubs, 10);
62+
const jackOfHearts = getCardValue("J♥");
63+
assertEquals(jackOfHearts, 10);
64+
const tenOfSpades = getCardValue("10♠");
65+
assertEquals(tenOfSpades, 10);
4866

4967
// Handle Ace (A):
5068
// Given a card with a rank of "A",
5169
// When the function is called with an Ace,
5270
// Then it should, by default, assume the Ace is worth 11 points, which is a common rule in blackjack.
71+
const aceOfHearts = getCardValue("A♥");
72+
assertEquals(aceOfHearts, 11);
5373

5474
// Handle Invalid Cards:
5575
// Given a card with an invalid rank (neither a number nor a recognized face card),
5676
// When the function is called with such a card,
5777
// Then it should throw an error indicating "Invalid card rank."
78+
const invalidCard = "1♠";
79+
assertEquals(invalidCard, "Error: Invalid card rank");
80+
const anotherInvalidCard = "Z♠";
81+
assertEquals(anotherInvalidCard, "Error: Invalid card rank");
82+
const emptyCard = "";
83+
assertEquals(emptyCard, "Error: Invalid card rank");

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

Lines changed: 46 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,55 @@
33
const getCardValue = require("../implement/3-get-card-value");
44

55
test("should return 11 for Ace of Spades", () => {
6-
const aceofSpades = getCardValue("A♠");
7-
expect(aceofSpades).toEqual(11);
6+
const aceOfSpades = getCardValue("A♠");
7+
expect(aceOfSpades).toEqual(11);
88
});
99

1010
// Case 2: Handle Number Cards (2-10):
11+
test("should return 9 for Nine of Hearts", () => {
12+
const nineOfHearts = getCardValue("9♥");
13+
expect(nineOfHearts).toEqual(9);
14+
});
15+
test("should return 5 for Five of Hearts", () => {
16+
const fiveOfHearts = getCardValue("5♥");
17+
expect(fiveOfHearts).toEqual(5);
18+
});
19+
test("should return 2 for Two of Diamonds", () => {
20+
const twoOfDiamonds = getCardValue("2♦");
21+
expect(twoOfDiamonds).toEqual(2);
22+
});
1123
// Case 3: Handle Face Cards (J, Q, K):
24+
test("should return 10 for King of Diamonds", () => {
25+
const kingOfDiamonds = getCardValue("K♦");
26+
expect(kingOfDiamonds).toEqual(10);
27+
});
28+
test("should return 10 for Queen of Clubs", () => {
29+
const queenOfClubs = getCardValue("Q♣");
30+
expect(queenOfClubs).toEqual(10);
31+
});
32+
test("should return 10 for Jack of Hearts", () => {
33+
const jackOfHearts = getCardValue("J♥");
34+
expect(jackOfHearts).toEqual(10);
35+
});
36+
test("should return 10 for Ten of Spades", () => {
37+
const tenOfSpades = getCardValue("10♠");
38+
expect(tenOfSpades).toEqual(10);
39+
});
1240
// Case 4: Handle Ace (A):
41+
test("should return 11 for Ace of Hearts", () => {
42+
const aceOfHearts = getCardValue("A♥");
43+
expect(aceOfHearts).toEqual(11);
44+
});
1345
// Case 5: Handle Invalid Cards:
46+
test("should throw an error for invalid card rank", () => {
47+
const invalidCard = getCardValue("1♠");
48+
expect(invalidCard).toEqual("Error: Invalid card rank");
49+
});
50+
test("should throw an error for another invalid card rank", () => {
51+
const anotherInvalidCard = getCardValue("Z♠");
52+
expect(anotherInvalidCard).toEqual("Error: Invalid card rank");
53+
});
54+
test("should throw an error for empty card string", () => {
55+
const emptyCard = getCardValue("");
56+
expect(emptyCard).toEqual("Error: Invalid card rank");
57+
});

0 commit comments

Comments
 (0)