Skip to content

Commit 05dc806

Browse files
committed
Validate card suit in getCardValue according to spec
1 parent 4483f93 commit 05dc806

File tree

1 file changed

+14
-2
lines changed

1 file changed

+14
-2
lines changed

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

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,17 @@
2323

2424
function getCardValue(card) {
2525
const rank = card.slice(0, -1);
26+
const suit = card.slice(-1);
27+
28+
const validSuits = ["♠", "♥", "♦", "♣"];
29+
30+
if (!validSuits.includes(suit)) {
31+
throw new Error("invalid card suit.");
32+
}
2633

2734
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))
35+
if (["K", "Q", "J"].includes(rank)) return 10;
36+
if (["2", "3", "4", "5", "6", "7", "8", "9", "10"].includes(rank))
3037
return Number(rank);
3138

3239
throw new Error("invalid card rank.");
@@ -62,6 +69,11 @@ try {
6269
console.error("Error was not thrown for invalid card");
6370
} catch (e) {}
6471

72+
try {
73+
getCardValue("A?");
74+
console.error("Error was not thrown for invalid suit");
75+
} catch (e) {}
76+
6577
// What other invalid card cases can you think of?
6678
assertEquals(getCardValue("2♦"), 2);
6779
assertEquals(getCardValue("5♥"), 5);

0 commit comments

Comments
 (0)