Skip to content

Commit 6f8616a

Browse files
committed
Function logic: added safeguards for incorrect suit and card value, added error for both if conditions do not pass. Added tests in node and jest for the incorrect suit case and error throwing. All tests pass.
1 parent 8fadb82 commit 6f8616a

3 files changed

Lines changed: 75 additions & 33 deletions

File tree

‎implement-and-rewrite-tests/implement-tests-with-node-test/3-get-card-value.test.js‎

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,4 +43,27 @@ describe ("Invalid cards", () => {
4343
test("Out of bound: 11♥", () => {
4444
assert.throws(() => getCardValue("11♥"), /Expected a number followed by a suit, but got "11♥"/);
4545
});
46+
});
47+
48+
describe("Invalid card suits", () => {
49+
test("Arbitrary non-card string", () => {
50+
assert.throws(
51+
() => getCardValue("2O"),
52+
/Expected card suit to be: "♠", "♥", "♦", "♣" but got "2O"/,
53+
"Expected clear error"
54+
);
55+
});
56+
test("Out of bounds: 1d", () => {
57+
assert.throws(
58+
() => getCardValue("8d"),
59+
/Expected card suit to be: "♠", "♥", "♦", "♣" but got "8d"/
60+
);
61+
});
62+
test("Out of bound: 11X", () => {
63+
assert.throws(
64+
() => getCardValue("9X"),
65+
//`Expected card suit to be: "♠", "♥", "♦", "♣" but got ${cardSuit}`
66+
/Expected card suit to be: "♠", "♥", "♦", "♣" but got "9X"/
67+
);
68+
});
4669
});

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

Lines changed: 34 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,41 @@
1-
// This problem involves playing cards: https://en.wikipedia.org/wiki/Standard_52-card_deck
1+
/*
2+
This problem involves playing cards: https://en.wikipedia.org/wiki/Standard_52-card_deck
23
3-
// Don't forget to write tests in implement-tests-with-node-test.
4-
5-
// Implement a function getCardValue, when given a string representing a playing card,
6-
// should return the numerical value of the card.
7-
8-
// A valid card string will contain a rank followed by the suit.
9-
// The rank can be one of the following strings:
10-
// "A", "2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K"
11-
// The suit can be one of the following emojis:
12-
// "♠", "♥", "♦", "♣"
13-
// For example: "A♠", "2♥", "10♥", "J♣", "Q♦", "K♦".
14-
15-
// When the card is an ace ("A"), the function should return 11.
16-
// When the card is a face card ("J", "Q", "K"), the function should return 10.
17-
// When the card is a number card ("2" to "10"), the function should return its numeric value.
18-
19-
// When the card string is invalid (not following the above format), the function should
20-
// throw an error.
21-
22-
// Acceptance criteria:
23-
// After you have implemented the function, write tests to cover all the cases, and
24-
// execute the code to ensure all tests pass.
4+
Implement a function getCardValue, when given a string representing a playing card,
5+
should return the numerical value of the card.
6+
A valid card string will contain a rank followed by the suit.
7+
The rank can be one of the following strings:
8+
"A", "2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K"
9+
The suit can be one of the following emojis:
10+
"♠", "♥", "♦", "♣"
11+
For example: "A♠", "2♥", "10♥", "J♣", "Q♦", "K♦".
12+
When the card is an ace ("A"), the function should return 11.
13+
When the card is a face card ("J", "Q", "K"), the function should return 10.
14+
When the card is a number card ("2" to "10"), the function should return its numeric value.
15+
When the card string is invalid (not following the above format), the function should
16+
throw an error.
17+
Acceptance criteria:
18+
After you have implemented the function, write tests to cover all the cases, and
19+
execute the code to ensure all tests pass.
20+
*/
2521

2622
export function getCardValue(card) {
27-
// TODO: Implement this function
2823
const cardValue = card.slice(0,-1);
24+
const cardSuit = card.slice(-1);
25+
const validSuits = ["♠", "♥", "♦", "♣"];
26+
const validNumberCards = ["2", "3", "4", "5", "6", "7", "8", "9", "10"]
27+
const validFaceCards = ["A", "K", "Q", "J"];
2928
const cardValueNumber = Number(cardValue)
30-
if (cardValue === "A") {
31-
return 11
32-
}
33-
else if (cardValue === "K" || cardValue === "Q" || cardValue === "J") {
34-
return 10
29+
if (!validNumberCards.includes(cardValue) && !validFaceCards.includes(cardValue)){
30+
throw new Error(`Expected a number followed by a suit, but got "${card}"`);
31+
} else if (!validSuits.includes(cardSuit)) {
32+
throw new Error(`Expected card suit to be: "♠", "♥", "♦", "♣" but got "${card}"`);
3533
}
36-
else if (cardValueNumber >= 2 && cardValueNumber <= 10) {
37-
return cardValueNumber
34+
if (cardValue === "A") {
35+
return 11;
36+
} else if (validFaceCards.includes(cardValue)) {
37+
return 10;
38+
} else {
39+
return cardValueNumber;
40+
}
3841
}
39-
throw new Error(`Expected a number followed by a suit, but got "${card}"`);
40-
}

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

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,4 +62,22 @@ describe("Invalid cards", () => {
6262
'Expected a number followed by a suit, but got "11♥"'
6363
);
6464
});
65+
});
66+
67+
describe("Invalid card suits", () => {
68+
test("Invalid suit: 2O", () => {
69+
expect(() => getCardValue("2O")).toThrow(
70+
'Expected card suit to be: "♠", "♥", "♦", "♣" but got "2O"'
71+
);
72+
});
73+
test("Invalid suit: 8d", () => {
74+
expect(() => getCardValue("8d")).toThrow(
75+
'Expected card suit to be: "♠", "♥", "♦", "♣" but got "8d"'
76+
);
77+
});
78+
test("Invalid suit: 9X", () => {
79+
expect(() => getCardValue("9X")).toThrow(
80+
'Expected card suit to be: "♠", "♥", "♦", "♣" but got "9X"'
81+
);
82+
});
6583
});

0 commit comments

Comments
 (0)