Skip to content

Commit 60f9cc7

Browse files
committed
get-card-value.js exercises completed
1 parent 780c9f9 commit 60f9cc7

File tree

1 file changed

+24
-1
lines changed

1 file changed

+24
-1
lines changed

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

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

2424
function getCardValue(card) {
25-
// TODO: Implement this function
25+
const ranks = ["A", "2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K"];
26+
const suits = ["♠", "♥", "♦", "♣"];
27+
const rankPart = card.slice(0, card.length - 1);
28+
const suitPart = card.slice(-1);
29+
30+
if (ranks.includes(rankPart) && suits.includes(suitPart)) {
31+
if (rankPart == "A") {
32+
return 11;
33+
} else if (rankPart == "J" || rankPart == "Q" || rankPart == "K") {
34+
return 10;
35+
} else {
36+
return Number(rankPart);
37+
}
38+
} else {
39+
throw new Error("Error");
40+
}
2641
}
2742

2843
// The line below allows us to load the getCardValue function into tests in other files.
@@ -41,6 +56,12 @@ function assertEquals(actualOutput, targetOutput) {
4156
// Examples:
4257
assertEquals(getCardValue("9♠"), 9);
4358

59+
assertEquals(getCardValue("10♣"), 10);
60+
assertEquals(getCardValue("J♥"), 10);
61+
assertEquals(getCardValue("K♥"), 10);
62+
assertEquals(getCardValue("A♦"), 11);
63+
assertEquals(getCardValue("2♦"), 2);
64+
4465
// Handling invalid cards
4566
try {
4667
getCardValue("invalid");
@@ -50,3 +71,5 @@ try {
5071
} catch (e) {}
5172

5273
// What other invalid card cases can you think of?
74+
assertEquals(getCardValue("♦♦"), "invalid"); // throws an error for this example
75+
assertEquals(getCardValue("12"), "Error");

0 commit comments

Comments
 (0)