Skip to content

Commit c8ff457

Browse files
node test pass for all value
1 parent 9771ba6 commit c8ff457

2 files changed

Lines changed: 21 additions & 10 deletions

File tree

‎implement-and-rewrite-tests/implement-tests-with-node-test/3-get-card-value.test.js‎

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,15 @@ test("Face card", () => {
2020
assert.equal(getCardValue("J♠"), 10);
2121
});
2222

23+
test("card is not a string", () => {
24+
assert.throws(
25+
() => {
26+
getCardValue(9);
27+
},
28+
{ message: "Input must be a String" }
29+
);
30+
});
31+
2332
test("Arbitrary non-card string", () => {
2433
assert.throws(
2534
() => getCardValue("invalid"),
@@ -46,13 +55,13 @@ test("Arbitrary non-card string", () => {
4655
);
4756

4857
assert.throws(
49-
() => getCardValue("7"),
58+
() => getCardValue("extra"),
5059
/Expected a number followed by a suit, but got invalid/,
5160
"Expected clear error"
5261
);
5362

5463
assert.throws(
55-
() => getCardValue(""),
64+
() => getCardValue("08♠"),
5665
/Expected a number followed by a suit, but got invalid/,
5766
"Expected clear error"
5867
);

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

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -25,25 +25,27 @@
2525

2626
export function getCardValue(card) {
2727
// TODO: Implement this function
28+
29+
if (typeof card != `string`) {
30+
throw new Error("Input must be a String");
31+
}
32+
const validRanks = ["2", "3", "4", "5", "6", "7", "8", "9", "10"];
2833
const rank = card.slice(0, -1);
2934
const suit = card.slice(-1);
3035

3136
if (suit != "♠" && suit != "♥" && suit != "♦" && suit != "♣") {
3237
throw new Error("Expected a number followed by a suit, but got invalid");
3338
}
34-
if (Number(rank) >= 2 && Number(rank) <= 10) {
35-
return rank;
39+
40+
if (validRanks.includes(rank)) {
41+
return Number(rank);
3642
}
3743

38-
if (card.slice(0, 1) === "A") {
44+
if (rank === "A") {
3945
return 11;
4046
}
4147

42-
if (
43-
card.slice(0, 1) === "J" ||
44-
card.slice(0, 1) === "Q" ||
45-
card.slice(0, 1) === "K"
46-
) {
48+
if (rank === "J" || rank === "Q" || rank === "K") {
4749
return 10;
4850
}
4951
throw new Error("Expected a number followed by a suit, but got invalid");

0 commit comments

Comments
 (0)