Skip to content

Commit c017a05

Browse files
committed
3-get-card-value.js completed
1 parent 9b3b9f6 commit c017a05

1 file changed

Lines changed: 49 additions & 1 deletion

File tree

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

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

2424
function getCardValue(card) {
25-
// TODO: Implement this function
25+
let regex = /^([2-9]|10|[AJQK])[♠♥♣♦]$/;
26+
const valid = regex.test(card);
27+
if (!valid) {
28+
throw new Error("Invalid card");
29+
}
30+
let rank = card.slice(0, -1);
31+
32+
if (rank === "A") {
33+
return 11;
34+
} else if (rank === "J" || rank === "Q" || rank === "K") {
35+
return 10;
36+
} else {
37+
rank = Number(rank);
38+
return rank;
39+
}
2640
}
2741

2842
// The line below allows us to load the getCardValue function into tests in other files.
@@ -40,6 +54,12 @@ function assertEquals(actualOutput, targetOutput) {
4054
// TODO: Write tests to cover all outcomes, including throwing errors for invalid cards.
4155
// Examples:
4256
assertEquals(getCardValue("9♠"), 9);
57+
assertEquals(getCardValue("A♠"), 11);
58+
assertEquals(getCardValue("10♥"), 10);
59+
assertEquals(getCardValue("2♥"), 2);
60+
assertEquals(getCardValue("K♠"), 10);
61+
assertEquals(getCardValue("J♣"), 10);
62+
assertEquals(getCardValue("Q♦"), 10);
4363

4464
// Handling invalid cards
4565
try {
@@ -52,3 +72,31 @@ try {
5272
}
5373

5474
// What other invalid card cases can you think of?
75+
76+
try {
77+
getCardValue(3);
78+
console.error("Error was not thrown for number 3");
79+
} catch (e) {
80+
console.log("Error thrown for invalid format");
81+
}
82+
83+
try {
84+
getCardValue("11K♠");
85+
console.error("Error format too long");
86+
} catch (e) {
87+
console.log("Error thrown for too many characters");
88+
}
89+
90+
try {
91+
getCardValue(-1);
92+
console.error("Error for negative number");
93+
} catch (e) {
94+
console.log("Error thrown for negative number");
95+
}
96+
97+
try {
98+
getCardValue("22♥");
99+
console.error("Error for string 22♥");
100+
} catch (e) {
101+
console.log("Error thrown for string 22♥");
102+
}

0 commit comments

Comments
 (0)