Skip to content

Commit 40ab054

Browse files
committed
Change getCardValue to pass tests
1 parent 8bfc6d0 commit 40ab054

1 file changed

Lines changed: 27 additions & 14 deletions

File tree

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

Lines changed: 27 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -21,34 +21,47 @@
2121
// After you have implemented the function, write tests to cover all the cases, and
2222
// execute the code to ensure all tests pass.
2323

24+
function isSuit(suit) {
25+
if (suit === "♠" || suit === "♥" || suit === "♦" || suit === "♣") {
26+
return true;
27+
}
28+
return false;
29+
}
30+
2431
function getCardValue(card) {
25-
const lastCharIndex = card.length - 1;
26-
const rank = card.slice(0, lastCharIndex);
27-
const suit = card.slice(lastCharIndex);
32+
// the length has to be 2 or 3 then we pass this check
2833

29-
if (rank === "10") {
30-
return 10;
34+
if (typeof card !== "string") {
35+
throw new Error(
36+
`Expected type for ${card} is string, but got ${typeof card}`
37+
);
3138
}
3239

33-
if (card.length > 2) {
34-
throw new Error("Invalid input length");
40+
if (card.length !== 2 && card.length !== 3) {
41+
throw new Error(`Invalid input length for ${card}`);
3542
}
3643

37-
if (!(suit === "♠" || suit === "♥" || suit === "♦" || suit === "♣")) {
44+
const lastCharIndex = card.length - 1;
45+
const rank = card.slice(0, lastCharIndex);
46+
const suit = card.slice(lastCharIndex);
47+
48+
if (!isSuit(suit)) {
3849
throw new Error(
39-
"Invalid second character detected, only suits(♠♥♦♣) are allowed, but got this character:",
40-
suit
50+
`Invalid last character detected, only suits(♠♥♦♣) are allowed, but got this character: ${suit}`
4151
);
4252
}
4353

44-
if (rank == "A") {
54+
if (rank === "A") {
4555
return 11;
46-
} else if (rank == "J" || rank == "Q" || rank == "K") {
56+
} else if (rank === "J" || rank === "Q" || rank === "K") {
4757
return 10;
48-
} else if (!isNaN(rank)) {
58+
} else if (!isNaN(rank) && Number(rank) >= 2 && Number(rank) <= 10) {
59+
// check if number and within bounds
4960
return Number(rank);
5061
} else {
51-
throw new Error("Invalid card");
62+
throw new Error(
63+
`${card} was passed into the function which is invalid, because one of the conditions failed`
64+
);
5265
}
5366
}
5467

0 commit comments

Comments
 (0)