Skip to content

Commit 5ad6379

Browse files
committed
completed 3-get-card-value.js and 3-get-card-value test.js with comments
1 parent 5cd25f5 commit 5ad6379

File tree

2 files changed

+101
-29
lines changed

2 files changed

+101
-29
lines changed

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

Lines changed: 63 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -23,48 +23,55 @@
2323

2424
// TODO: Implement this function
2525
function getCardValue(card) {
26-
const validRanks = ["A", "2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K"];
27-
const validSuits = ["♠", "♥", "♦", "♣"];
28-
//Validation
29-
// Must be a string
30-
if (typeof card !== "string") {
31-
throw Error("Invalid card");
26+
// 1. declare variables in an array to use for validation
27+
const validRanks = [
28+
"A",
29+
"2",
30+
"3",
31+
"4",
32+
"5",
33+
"6",
34+
"7",
35+
"8",
36+
"9",
37+
"10",
38+
"J",
39+
"Q",
40+
"K",
41+
];
42+
const validSuits = ["♠", "♥", "♦", "♣"];
43+
//2. Check that input is a string
44+
if (typeof card !== "string") {
45+
throw new Error("Invalid card");
3246
}
33-
// Must be at least 2 characters and maximum of 3
34-
//
35-
//
36-
}
37-
// ---------- INVALID CHECKS FIRST ----------
38-
39-
// 1. Must be a string
40-
41-
42-
// 2. Must have at least 2 characters (rank + suit)
43-
if (card.length < 2) {
47+
// 3. Must have at least 2 characters but 3 or less (rank and suit)
48+
if (card.length < 2 || card.length > 3) {
4449
throw new Error("Invalid card");
4550
}
4651

47-
// 3. Extract suit and rank
52+
// 4. Defining the suit and rank
53+
//Select character index -1, the last character only.
4854
const suit = card.slice(-1);
55+
//select everything from index 0 and stop before the last character
4956
const rank = card.slice(0, -1);
5057

51-
// 4. Suit must be valid
58+
// 5. Checking suit validity
59+
//Checks if suit is in the array
5260
if (!validSuits.includes(suit)) {
5361
throw new Error("Invalid card");
5462
}
55-
56-
// 5. Rank must be valid
63+
// 6. Checking rank validity
64+
//Checks if rank is in the array
5765
if (!validRanks.includes(rank)) {
5866
throw new Error("Invalid card");
5967
}
60-
61-
// ---------- ALL INPUT IS NOW VALID ----------
62-
68+
//7. Determines the output values for special cards
6369
if (rank === "A") return 11;
6470
if (["J", "Q", "K"].includes(rank)) return 10;
65-
71+
// 8. Otherwise it returns the number of the card
6672
return Number(rank);
6773
}
74+
6875
// The line below allows us to load the getCardValue function into tests in other files.
6976
// This will be useful in the "rewrite tests with jest" step.
7077
module.exports = getCardValue;
@@ -80,13 +87,43 @@ function assertEquals(actualOutput, targetOutput) {
8087
// TODO: Write tests to cover all outcomes, including throwing errors for invalid cards.
8188
// Examples:
8289
assertEquals(getCardValue("9♠"), 9);
90+
assertEquals(getCardValue("A♠"), 11);
91+
assertEquals(getCardValue("Q♥"), 10);
92+
assertEquals(getCardValue("K♦"), 10);
93+
assertEquals(getCardValue("J♣"), 10);
8394

8495
// Handling invalid cards
8596
try {
8697
getCardValue("invalid");
8798

8899
// This line will not be reached if an error is thrown as expected
89100
console.error("Error was not thrown for invalid card");
90-
} catch (e) {}
101+
} catch (error) {}
91102

92103
// What other invalid card cases can you think of?
104+
try {
105+
getCardValue("789");
106+
107+
// This line will not be reached if an error is thrown as expected
108+
console.error("Error was not thrown for invalid card");
109+
} catch (error) {}
110+
111+
try {
112+
getCardValue("");
113+
console.error("Error was not thrown for invalid card");
114+
} catch (e) {}
115+
116+
try {
117+
getCardValue("♠♥♦♣");
118+
console.error("Error was not thrown for invalid card");
119+
} catch (error) {}
120+
121+
try {
122+
getCardValue("5$");
123+
console.error("Error was not thrown for invalid card");
124+
} catch (error) {}
125+
126+
try {
127+
getCardValue("£50");
128+
console.error("Error was not thrown for invalid card");
129+
} catch (error) {}

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

Lines changed: 38 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,48 @@ const getCardValue = require("../implement/3-get-card-value");
66

77
// Case 1: Ace (A)
88
test(`Should return 11 when given an ace card`, () => {
9-
expect(getCardValue("A♠")).toEqual(11);
10-
});
11-
9+
expect(getCardValue("A♠")).toEqual(11)});
1210
// Suggestion: Group the remaining test data into these categories:
1311
// Number Cards (2-10)
12+
test(`Should return a number given 9 card` , ()=> {
13+
expect(getCardValue("9♠")).toEqual(9)});
1414
// Face Cards (J, Q, K)
15+
test(`Should return 10 when given a Queen card`, () => {
16+
expect(getCardValue("Q♥")).toEqual(10);
17+
});
18+
test(`Should return 10 when given a King card`, () => {
19+
expect(getCardValue("K♦")).toEqual(10);
20+
});
21+
22+
test(`Should return 10 when given a Jack card`, () => {
23+
expect(getCardValue("J♣")).toEqual(10);
24+
});
25+
1526
// Invalid Cards
27+
test('Should throw "Invalid card" for "invalid"', () => {
28+
expect(() => getCardValue("invalid")).toThrow("Invalid card");
29+
});
30+
31+
test('Should throw "Invalid card" for input "789"', () => {
32+
expect(() => getCardValue("789")).toThrow("Invalid card");
33+
});
34+
35+
test('Should throw "Invalid card" for input ""', () => {
36+
expect(() => getCardValue("")).toThrow("Invalid card");
37+
});
38+
39+
test('Should throw "Invalid card" for input "♠♥♦♣"', () => {
40+
expect(() => getCardValue("♠♥♦♣")).toThrow("Invalid card");
41+
});
42+
43+
test('Should throw "Invalid card" for input "♠♥♦♣"', () => {
44+
expect(() => getCardValue("5$")).toThrow("Invalid card");
45+
});
46+
47+
test('Should throw "Invalid card" for input "♠♥♦♣"', () => {
48+
expect(() => getCardValue("£50")).toThrow("Invalid card");
49+
});
50+
1651

1752
// To learn how to test whether a function throws an error as expected in Jest,
1853
// please refer to the Jest documentation:

0 commit comments

Comments
 (0)