Skip to content

Commit 3975e54

Browse files
committed
Refactor getCardValue function to improve error handling and support for numeric ranks
1 parent a18d6db commit 3975e54

File tree

1 file changed

+11
-12
lines changed

1 file changed

+11
-12
lines changed

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

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -8,22 +8,21 @@
88
// write one test at a time, and make it pass, build your solution up methodically
99
// just make one change at a time -- don't rush -- programmers are deep and careful thinkers
1010
function getCardValue(card) {
11-
const rank = card.slice(0, -1); // remove the suit symbol
11+
const rank = card.slice(0, -1);
1212

1313
if (rank === "A") {
1414
return 11;
15-
}
16-
17-
if (["K", "Q", "J", "10"].includes(rank)) {
15+
} else if (rank === "K" || rank === "Q" || rank === "J" || rank === "10") {
1816
return 10;
19-
}
17+
} else {
18+
const numericRank = parseInt(rank, 10);
2019

21-
const numeric = parseInt(rank);
22-
if (!isNaN(numeric)) {
23-
return numeric;
20+
if (!isNaN(numericRank) && numericRank >= 2 && numericRank <= 9) {
21+
return numericRank;
22+
} else {
23+
throw new Error(`Invalid or unrecognized card rank: "${rank}"`);
24+
}
2425
}
25-
26-
throw new Error("Invalid card rank.");
2726
}
2827

2928
// The line below allows us to load the getCardValue function into tests in other files.
@@ -88,7 +87,7 @@ try {
8887
console.assert(false, "Expected an error for invalid card rank");
8988
} catch (error) {
9089
console.assert(
91-
error.message === "Invalid card rank.",
92-
`Expected error message "Invalid card rank.", got "${error.message}"`
90+
error.message === 'Invalid or unrecognized card rank: "Z"',
91+
`Expected error message 'Invalid or unrecognized card rank: "Z"', got "${error.message}"`
9392
);
9493
}

0 commit comments

Comments
 (0)