-
-
Notifications
You must be signed in to change notification settings - Fork 337
Expand file tree
/
Copy path3-get-card-value.js
More file actions
99 lines (86 loc) · 2.44 KB
/
3-get-card-value.js
File metadata and controls
99 lines (86 loc) · 2.44 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
function getCardValue(card) {
// Ensure that the last char is a suit, otherwise throw an error
const suits = ["♠", "♥", "♦", "♣"];
if (!suits.includes(card.slice(-1))) {
throw new Error("Please add the suit to the card face i.e. '5♥' ");
}
const rank = card.slice(0, -1);
const validRank = [
"A",
"2",
"3",
"4",
"5",
"6",
"7",
"8",
"9",
"10",
"J",
"Q",
"K",
];
// Throw an error if an invalid card is entered
if (!validRank.includes(rank)) {
throw new Error("Please enter a valid card face");
}
// Return value if card rank is a number
if (parseInt(rank)) {
return parseInt(rank);
}
// Return 11 for "A" and 10 for "J", "Q", "K"
if (rank === "A") {
return 11;
} else {
return 10;
}
}
// The line below allows us to load the getCardValue function into tests in other files.
// This will be useful in the "rewrite tests with jest" step.
module.exports = getCardValue;
// Helper functions to make our assertions easier to read.
function assertEquals(actualOutput, targetOutput) {
console.assert(
actualOutput === targetOutput,
`Expected ${actualOutput} to equal ${targetOutput}`
);
}
// ============= Valid Card Tests ===========
assertEquals(getCardValue("A♠"), 11);
assertEquals(getCardValue("K♥"), 10);
assertEquals(getCardValue("Q♦"), 10);
assertEquals(getCardValue("J♣"), 10);
assertEquals(getCardValue("9♠"), 9);
assertEquals(getCardValue("2♥"), 2);
// ============= Invalid Card Tests ===========
let invalidCard = "invalid";
try {
getCardValue(invalidCard);
console.error(`Error was not thrown for an invalid card (${invalidCard})`);
} catch (e) {}
invalidCard = 7;
try {
getCardValue(invalidCard);
console.error(`Error was not thrown for an invalid card (${invalidCard})`);
} catch (e) {}
invalidCard = "";
try {
getCardValue(invalidCard);
console.error(`Error was not thrown for an invalid card (${invalidCard})`);
} catch (e) {}
invalidCard = "AA♠";
try {
getCardValue(invalidCard);
console.error(`Error was not thrown for an invalid card (${invalidCard})`);
} catch (e) {}
invalidCard = "10♠♦";
try {
getCardValue(invalidCard);
console.error(`Error was not thrown for an invalid card (${invalidCard})`);
} catch (e) {}
invalidCard = "10";
try {
console.log(getCardValue(invalidCard));
getCardValue(invalidCard);
console.error(`Error was not thrown for an invalid card (${invalidCard})`);
} catch (e) {}