Skip to content

Commit bae2cd3

Browse files
committed
Update 1-implment-and-rewrite-tests/implement/3-get-card-value.js
1 parent 163c1d0 commit bae2cd3

2 files changed

Lines changed: 78 additions & 35 deletions

File tree

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

Lines changed: 60 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -20,56 +20,82 @@
2020
// Acceptance criteria:
2121
// After you have implemented the function, write tests to cover all the cases, and
2222
// execute the code to ensure all tests pass.
23-
function generateValidCards() {
24-
const ranks = "A,2,3,4,5,6,7,8,9,10,J,Q,K".split(",");
25-
const suites = "♠,♥,♦,♣".split(",");
26-
const validCards = new Set();
27-
for (const rank of ranks) {
28-
for (const suite of suites) {
29-
validCards.add(rank + suite);
30-
}
31-
}
32-
return validCards;
33-
}
3423

3524
function getCardValue(card) {
3625
// TODO: Implement this function
37-
const validCards = generateValidCards();
38-
if (!validCards.has(card)) {
39-
throw new Error("invalid");
26+
const cardRanks = "A,2,3,4,5,6,7,8,9,10,J,Q,K".split(",");
27+
const cardSuits = "♠,♥,♦,♣".split(",");
28+
29+
// Throw error if card is empty
30+
if (!card) throw new Error(`Invalid card`);
31+
32+
const cardSuit = card[card.length - 1];
33+
// Throw error is card suit is not valid
34+
if (!cardSuits.includes(cardSuit)) {
35+
throw new Error(`Invalid card suit`);
4036
}
4137

42-
let cardValue = card.slice(0, -1);
43-
if (cardValue === "A") return 11;
44-
if (cardValue === "J" || cardValue === "Q" || cardValue === "K") return 10;
45-
return Number(cardValue);
38+
const cardRank = card.slice(0, -1);
39+
// Throw error is cardRank is not valid
40+
if (!cardRanks.includes(cardRank)) {
41+
throw new Error(`Invalid card rank`);
42+
}
43+
44+
// Return the card value based on given rank
45+
if (cardRank === "A") return 11;
46+
if (["J", "Q", "K"].includes(cardRank)) return 10;
47+
return Number(cardRank);
4648
}
4749

4850
// The line below allows us to load the getCardValue function into tests in other files.
4951
// This will be useful in the "rewrite tests with jest" step.
5052
module.exports = getCardValue;
5153

5254
// Helper functions to make our assertions easier to read.
53-
function assertEquals(card, targetOutput) {
55+
function assertEquals(actualOutput, targetOutput) {
56+
console.assert(
57+
actualOutput === targetOutput,
58+
`Expected ${actualOutput} to equal ${targetOutput}`
59+
);
60+
}
61+
62+
// TODO: Write tests to cover all outcomes, including throwing errors for invalid cards.
63+
// Examples:
64+
assertEquals(getCardValue("9♠"), 9);
65+
assertEquals(getCardValue("J♥"), 10);
66+
assertEquals(getCardValue("Q♣"), 10);
67+
assertEquals(getCardValue("K♠"), 10);
68+
assertEquals(getCardValue("A♥"), 11);
69+
assertEquals(getCardValue("10♦"), 10);
70+
71+
// Handling invalid cards
72+
function assertErrors(func, errorMessage) {
5473
try {
55-
console.log(`Card: ${card}`);
56-
const actualOutput = getCardValue(card);
74+
func();
75+
console.error("Error was not thrown for invalid card 😢");
76+
} catch (error) {
5777
console.assert(
58-
actualOutput === targetOutput,
59-
`Expected ${actualOutput} to equal ${targetOutput}`
78+
error.message.includes(errorMessage),
79+
`Expected error: ${errorMessage}, got: ${error.message}`
6080
);
61-
console.error("Error was not thrown for invalid card 😢");
62-
} catch (e) {
6381
console.log("Error thrown for invalid card 🎉");
6482
}
65-
console.log("-".repeat(45));
6683
}
6784

68-
// TODO: Write tests to cover all outcomes, including throwing errors for invalid cards.
69-
assertEquals("9♠", 9);
70-
assertEquals("A", 11);
71-
assertEquals("Q", 10);
72-
assertEquals("5", 5);
73-
assertEquals("A♣", 11);
74-
assertEquals("Q♣", 11);
75-
assertEquals("A♦", 10);
85+
function testInvalidCards() {
86+
const invalidCardCases = [
87+
["A", "Invalid card suit"],
88+
["11♠", "Invalid card rank"],
89+
["N♠", "Invalid card rank"],
90+
["7K", "Invalid card suit"],
91+
["", "Invalid card"],
92+
];
93+
94+
invalidCardCases.forEach(([card, message]) => {
95+
console.log("-".repeat(50));
96+
console.log(`Card: ${card}, Message: ${message}`);
97+
assertErrors(() => getCardValue(card), message);
98+
});
99+
}
100+
101+
testInvalidCards();

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

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,24 @@ test(`Should return 11 when given an ace card`, () => {
99
expect(getCardValue("A♠")).toEqual(11);
1010
});
1111

12+
test(`Should return 10 when given an a face card of J, Q or K`, () => {
13+
expect(getCardValue("J♠")).toEqual(10);
14+
expect(getCardValue("Q♠")).toEqual(10);
15+
expect(getCardValue("K♠")).toEqual(10);
16+
});
17+
18+
test(`Should return number when given an a face card (>= 2 && < 10)`, () => {
19+
expect(getCardValue("2♠")).toEqual(2);
20+
expect(getCardValue("6♠")).toEqual(6);
21+
expect(getCardValue("9♠")).toEqual(9);
22+
});
23+
24+
test(`Should return an error for an invalid card`, () => {
25+
expect(getCardValue("2")).toThrowError();
26+
expect(getCardValue("Q")).toThrowError();
27+
expect(getCardValue("5")).toThrowError();
28+
});
29+
1230
// Suggestion: Group the remaining test data into these categories:
1331
// Number Cards (2-10)
1432
// Face Cards (J, Q, K)
@@ -17,4 +35,3 @@ test(`Should return 11 when given an ace card`, () => {
1735
// To learn how to test whether a function throws an error as expected in Jest,
1836
// please refer to the Jest documentation:
1937
// https://jestjs.io/docs/expect#tothrowerror
20-

0 commit comments

Comments
 (0)