-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path3-get-card-value.js
More file actions
101 lines (85 loc) · 3.09 KB
/
Copy path3-get-card-value.js
File metadata and controls
101 lines (85 loc) · 3.09 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
100
101
// This problem involves playing cards: https://en.wikipedia.org/wiki/Standard_52-card_deck
//
// ORIGINAL QUESTION:
// Implement a function getCardValue, when given a string representing a playing card,
// should return the numerical value of the card.
//
// A valid card string will contain a rank followed by the suit.
// The rank can be one of the following strings:
// "A", "2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K"
// The suit can be one of the following emojis:
// "♠", "♥", "♦", "♣"
// For example: "A♠", "2♥", "10♥", "J♣", "Q♦", "K♦".
//
// When the card is an ace ("A"), the function should return 11.
// When the card is a face card ("J", "Q", "K"), the function should return 10.
// When the card is a number card ("2" to "10"), the function should return its numeric value.
//
// When the card string is invalid (not following the above format), the function should
// throw an error.
//
// Acceptance criteria:
// After you have implemented the function, write tests to cover all the cases, and
// execute the code to ensure all tests pass.
function getCardValue(card) {
// 1. Separate the rank from the suit emoji
const rank = card.slice(0, -1);
const suit = card.slice(-1);
// 2. Validate the suit
const validSuits = ["♠", "♥", "♦", "♣"];
if (!validSuits.includes(suit)) {
throw new Error("Invalid suit");
}
// 3. Calculate value based on the rank
if (rank === "A") {
return 11;
}
if (rank === "J" || rank === "Q" || rank === "K") {
return 10;
}
// 4. Handle number cards (2-10)
const numericRank = parseInt(rank, 10);
// numericRank.toString() === rank makes sure rank was really a clean number string
// Example: parseInt("9", 10) -> 9, "9" === "9" ✅
// Example: parseInt("9x", 10) -> 9, "9" === "9x" ❌ (so invalid)
if (numericRank >= 2 && numericRank <= 10 && numericRank.toString() === rank) {
return numericRank;
}
// 5. If it reaches here, the rank was invalid
throw new Error("Invalid rank");
}
// Export for other test files (Jest step later)
module.exports = getCardValue;
// Helper function for testing (console.assert style)
function assertEquals(actualOutput, targetOutput) {
console.assert(
actualOutput === targetOutput,
`Expected ${actualOutput} to equal ${targetOutput}`
);
}
// =======================
// TESTS
// =======================
// Test Aces and Face Cards
assertEquals(getCardValue("A♠"), 11);
assertEquals(getCardValue("J♥"), 10);
assertEquals(getCardValue("Q♦"), 10);
assertEquals(getCardValue("K♣"), 10);
// Test Numbers
assertEquals(getCardValue("9♠"), 9);
assertEquals(getCardValue("10♥"), 10);
assertEquals(getCardValue("2♣"), 2);
// Test Invalid Inputs (should throw errors)
try {
getCardValue("invalid");
console.error("Error was not thrown for 'invalid'");
} catch (e) {}
try {
getCardValue("1♠"); // There is no 1 card, only A,2..10,J,Q,K
console.error("Error was not thrown for '1♠'");
} catch (e) {}
try {
getCardValue("A?"); // invalid suit
console.error("Error was not thrown for 'A?'");
} catch (e) {}
console.log("Tests complete!");