Skip to content

Commit 36e1138

Browse files
committed
validate string value before converting its value
1 parent 338eadc commit 36e1138

File tree

2 files changed

+7
-6
lines changed

2 files changed

+7
-6
lines changed

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

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,11 @@ function getCardValue(card) {
1515
return 11;
1616
}
1717

18-
const numeric = Number(rank);
19-
if (Number.isInteger(numeric) && numeric >= 2 && numeric <= 10) {
20-
return numeric;
21-
}
18+
// Strictly validate rank for number cards (2-10)
19+
const validRanks = ["2", "3", "4", "5", "6", "7", "8", "9", "10"];
20+
if (validRanks.includes(rank)) {
21+
return Number(rank);
22+
}
2223

2324
if (rank === "J" || rank === "Q" || rank === "K") {
2425
return 10;

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,9 +47,9 @@ test("should return correct value for all number cards 2-10", () => {
4747
});
4848

4949
test("should handle numeric-literal edge cases", () => {
50-
expect(getCardValue("0x02♠")).toEqual(2);
50+
expect(getCardValue("0x02♠")).toBeUndefined();
5151
expect(getCardValue("2.1♠")).toBeUndefined();
52-
expect(getCardValue("0002♠")).toEqual(2);
52+
expect(getCardValue("0002♠")).toBeUndefined();
5353
});
5454

5555
test("should return 10 for all face cards", () => {

0 commit comments

Comments
 (0)