Skip to content

Commit d52b78f

Browse files
committed
fixed task error
1 parent f1de0d9 commit d52b78f

File tree

1 file changed

+23
-34
lines changed

1 file changed

+23
-34
lines changed

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

Lines changed: 23 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -22,59 +22,48 @@
2222
// execute the code to ensure all tests pass.
2323

2424
function getCardValue(card) {
25-
const rank = card.slice(0, -1);
26-
const suit = card.slice(-1);
27-
const validSuits = ["♠", "♥", "♦", "♣"];
28-
const validRanks = [
29-
"A",
30-
"2",
31-
"3",
32-
"4",
33-
"5",
34-
"6",
35-
"7",
36-
"8",
37-
"9",
38-
"10",
39-
"J",
40-
"Q",
41-
"K",
42-
];
43-
44-
if (!validSuits.includes(suit) || !validRanks.includes(rank)) {
25+
const validSuits = ["♠", "♣", "♥", "♦"];
26+
27+
if (typeof card !== "string" || card.length < 2) {
4528
throw new Error("Invalid card");
4629
}
30+
31+
const suit = card.slice(-1);
32+
const rank = card.slice(0, -1);
33+
34+
if (!validSuits.includes(suit)) {
35+
throw new Error("invalid card");
36+
}
37+
4738
if (rank === "A") {
4839
return 11;
4940
}
41+
5042
if (rank === "J" || rank === "Q" || rank === "K") {
5143
return 10;
5244
}
53-
return parseInt(rank);
45+
46+
const numberValue = Number(rank);
47+
48+
if (!Number.isNaN(numberValue) && numberValue >= 2 && numberValue <= 10) {
49+
return numberValue;
50+
}
51+
52+
throw new Error("Invalid card");
5453
}
5554

55+
module.exports = getCardValue;
56+
5657
// The line below allows us to load the getCardValue function into tests in other files.
5758
// This will be useful in the "rewrite tests with jest" step.
58-
module.exports = getCardValue;
5959

6060
// Helper functions to make our assertions easier to read.
61-
function assertEquals(actualOutput, targetOutput) {
62-
console.assert(
63-
actualOutput === targetOutput,
64-
`Expected ${actualOutput} to equal ${targetOutput}`
65-
);
66-
}
6761

6862
// TODO: Write tests to cover all outcomes, including throwing errors for invalid cards.
6963
// Examples:
70-
assertEquals(getCardValue("9♠"), 9);
7164

7265
// Handling invalid cards
73-
try {
74-
getCardValue("invalid");
7566

76-
// This line will not be reached if an error is thrown as expected
77-
console.error("Error was not thrown for invalid card");
78-
} catch (e) {}
67+
// This line will not be reached if an error is thrown as expected
7968

8069
// What other invalid card cases can you think of?

0 commit comments

Comments
 (0)