Skip to content

Commit 29da7a4

Browse files
committed
Implement getCardValue with tests for all card ranks
1 parent 29b6533 commit 29da7a4

File tree

1 file changed

+16
-1
lines changed

1 file changed

+16
-1
lines changed

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

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

2424
function getCardValue(card) {
25-
// TODO: Implement this function
25+
const rank = card.slice(0, -1);
26+
27+
if (rank === "A") return 11;
28+
if (["K", "Q", "J", "10"].includes(rank)) return 10;
29+
if (["2", "3", "4", "5", "6", "7", "8", "9"].includes(rank))
30+
return Number(rank);
31+
32+
throw new Error("invalid card rank.");
2633
}
34+
// TODO: Implement this function
2735

2836
// The line below allows us to load the getCardValue function into tests in other files.
2937
// This will be useful in the "rewrite tests with jest" step.
@@ -40,6 +48,11 @@ function assertEquals(actualOutput, targetOutput) {
4048
// TODO: Write tests to cover all outcomes, including throwing errors for invalid cards.
4149
// Examples:
4250
assertEquals(getCardValue("9♠"), 9);
51+
assertEquals(getCardValue("A♣"), 11);
52+
assertEquals(getCardValue("J♥"), 10);
53+
assertEquals(getCardValue("Q♦"), 10);
54+
assertEquals(getCardValue("K♠"), 10);
55+
assertEquals(getCardValue("10♣"), 10);
4356

4457
// Handling invalid cards
4558
try {
@@ -50,3 +63,5 @@ try {
5063
} catch (e) {}
5164

5265
// What other invalid card cases can you think of?
66+
assertEquals(getCardValue("2♦"), 2);
67+
assertEquals(getCardValue("5♥"), 5);

0 commit comments

Comments
 (0)