Skip to content

Commit 3f7c3f8

Browse files
committed
enhancement: Implement getCardValue function to return correct card values
1 parent 7eab738 commit 3f7c3f8

File tree

1 file changed

+9
-11
lines changed

1 file changed

+9
-11
lines changed

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

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -23,17 +23,15 @@
2323

2424
function getCardValue(card) {
2525
// TODO: Implement this function
26-
if (
27-
card.length < 2 ||
28-
card.length > 3 ||
29-
!["♠", "♥", "♦", "♣"].includes(card.slice(-1))
30-
)
31-
throw new Error("invalid suit");
32-
if (card[0] === "A") return 11;
33-
if (["J", "Q", "K"].includes(card[0])) return 10;
34-
if (["2", "3", "4", "5", "6", "7", "8", "9", "10"].includes(card[0]))
35-
return card[0]; // the parseint() or Number() can be used to convert the string to a number
36-
throw new Error("invalid rank");
26+
const cardRank = card.slice(0, -1);
27+
const cardSuit = card.slice(-1);
28+
if (card.length < 2 || !["♠", "♥", "♦", "♣"].includes(cardSuit))
29+
throw new Error("invalid card suit");
30+
if (cardRank === "A") return 11;
31+
if (["J", "Q", "K"].includes(cardRank)) return 10;
32+
if (["2", "3", "4", "5", "6", "7", "8", "9", "10"].includes(cardRank))
33+
return cardRank; // the parseint() or Number() can be used to convert the string to a number
34+
throw new Error("invalid card rank");
3735
}
3836

3937
// The line below allows us to load the getCardValue function into tests in other files.

0 commit comments

Comments
 (0)