Skip to content

Commit caafa56

Browse files
committed
re improved the task
1 parent d52b78f commit caafa56

File tree

1 file changed

+34
-23
lines changed

1 file changed

+34
-23
lines changed

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

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

2424
function getCardValue(card) {
25-
const validSuits = ["♠", "♣", "♥", "♦"];
26-
27-
if (typeof card !== "string" || card.length < 2) {
28-
throw new Error("Invalid card");
29-
}
30-
31-
const suit = card.slice(-1);
3225
const rank = card.slice(0, -1);
33-
34-
if (!validSuits.includes(suit)) {
35-
throw new Error("invalid card");
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)) {
45+
throw new Error("Invalid card");
3646
}
37-
3847
if (rank === "A") {
3948
return 11;
4049
}
41-
4250
if (rank === "J" || rank === "Q" || rank === "K") {
4351
return 10;
4452
}
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");
53+
return parseInt(rank);
5354
}
5455

55-
module.exports = getCardValue;
56-
5756
// The line below allows us to load the getCardValue function into tests in other files.
5857
// 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+
}
6167

6268
// TODO: Write tests to cover all outcomes, including throwing errors for invalid cards.
6369
// Examples:
70+
assertEquals(getCardValue("9♠"), 9);
6471

6572
// Handling invalid cards
73+
try {
74+
getCardValue("invalid");
6675

67-
// This line will not be reached if an error is thrown as expected
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) {}
6879

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

0 commit comments

Comments
 (0)