Skip to content

Commit 49b11ff

Browse files
committed
Adding in code and testing for 3-get-card-value
1 parent 5b755ba commit 49b11ff

2 files changed

Lines changed: 44 additions & 1 deletion

File tree

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

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,26 @@
2222
// execute the code to ensure all tests pass.
2323

2424
function getCardValue(card) {
25+
//check for suit
26+
const suits = "♥♦♣♠";
27+
if (!suits.includes(card.slice(-1))){
28+
return "Invalid"
29+
};
30+
// check for face card or number
31+
const cardFace = "AKJQ";
32+
const numberCards = ["2", "3", "4", "5", "6", "7", "8", "9", "10"];
33+
if (!cardFace.includes(card.slice(0, -1)) && !numberCards.includes(card.slice(0, -1))){
34+
return "Invalid"
35+
};
36+
37+
const removeSuit = card.slice (0, -1);
38+
if (removeSuit === "A") {
39+
return 11
40+
} else if (removeSuit === "J" || removeSuit === "Q" || removeSuit === "K" ){
41+
return 10;
42+
}
43+
return Number(removeSuit);
44+
2545
// TODO: Implement this function
2646
}
2747

@@ -35,7 +55,7 @@ function assertEquals(actualOutput, targetOutput) {
3555
actualOutput === targetOutput,
3656
`Expected ${actualOutput} to equal ${targetOutput}`
3757
);
38-
}
58+
};
3959

4060
// TODO: Write tests to cover all outcomes, including throwing errors for invalid cards.
4161
// Examples:

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

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,28 @@ const getCardValue = require("../implement/3-get-card-value");
77
// Case 1: Ace (A)
88
test(`Should return 11 when given an ace card`, () => {
99
expect(getCardValue("A♠")).toEqual(11);
10+
expect(getCardValue("A♦")).toEqual(11);
11+
});
12+
13+
// Case 2: Face cards (J, Q, K)
14+
test(`Should return 10 when given a face card`, () => {
15+
expect(getCardValue("K♦")).toEqual(10);
16+
expect(getCardValue("Q♥")).toEqual(10);
17+
expect(getCardValue("J♣")).toEqual(10);
18+
});
19+
20+
// Case 3: Number cards (1-10)
21+
test(`Should return number value when given a number card`, () => {
22+
expect(getCardValue("2♦")).toEqual(2);
23+
expect(getCardValue("8♥")).toEqual(8);
24+
expect(getCardValue("10♥")).toEqual(10);
25+
});
26+
27+
// Case 4: Invalid cards
28+
test(`Should return Invalid when given invalid cards`, () => {
29+
expect(getCardValue("10")).toEqual("Invalid");
30+
expect(getCardValue("ka9")).toEqual("Invalid");
31+
expect(getCardValue("13♥")).toEqual("Invalid");
1032
});
1133

1234
// Suggestion: Group the remaining test data into these categories:
@@ -18,3 +40,4 @@ test(`Should return 11 when given an ace card`, () => {
1840
// please refer to the Jest documentation:
1941
// https://jestjs.io/docs/expect#tothrowerror
2042

43+
// "♠", "♥", "♦", "♣"

0 commit comments

Comments
 (0)