Skip to content

Commit 3be8b88

Browse files
authored
Implement getCardValue function with error handling
Implemented the getCardValue function to determine the value of a card based on its rank and suit. Added error handling for invalid cards and wrote initial test cases.
1 parent f54aa1f commit 3be8b88

File tree

1 file changed

+22
-2
lines changed

1 file changed

+22
-2
lines changed

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

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,27 @@
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+
const suit = card.slice(-1);
27+
28+
const vaildSuits = [""", "♥", "♦", "♣"];
29+
const vailRanks = ["A","2","3","4","5","6","7","8","9","10","J","Q","k"];
30+
31+
if (!vaildSuits.includes) || !validRanks.inculdes(rank){
32+
trow new Error("Invaild card");
33+
}
34+
if (rank === "A") return 11;
35+
if (rank === "J" || rank === "Q" || rank === "K") return 10;
36+
37+
return Number(rank);
2638
}
2739

40+
module.exports = getCardValue;
41+
42+
2843
// The line below allows us to load the getCardValue function into tests in other files.
2944
// This will be useful in the "rewrite tests with jest" step.
30-
module.exports = getCardValue;
45+
3146

3247
// Helper functions to make our assertions easier to read.
3348
function assertEquals(actualOutput, targetOutput) {
@@ -40,10 +55,15 @@ function assertEquals(actualOutput, targetOutput) {
4055
// TODO: Write tests to cover all outcomes, including throwing errors for invalid cards.
4156
// Examples:
4257
assertEquals(getCardValue("9♠"), 9);
58+
//Test
59+
assertEquals(getCardvalue("A♠"), 11);
60+
assertEquals(getCardEqual("J♣), 10);
61+
assertEquals(getCardequal("10♥"), 10);
4362

4463
// Handling invalid cards
4564
try {
4665
getCardValue("invalid");
66+
4767

4868
// This line will not be reached if an error is thrown as expected
4969
console.error("Error was not thrown for invalid card");

0 commit comments

Comments
 (0)