Skip to content

Commit 79fd501

Browse files
Add validation for card input in getCardValue function
1 parent 6c0b2c0 commit 79fd501

File tree

1 file changed

+8
-4
lines changed

1 file changed

+8
-4
lines changed

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

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,9 @@
99
// just make one change at a time -- don't rush -- programmers are deep and careful thinkers
1010
const assert = require("assert");
1111
function getCardValue(card) {
12+
if(!/[]$/.test(card)){
13+
throw new Error("Invalid card")
14+
}
1215
let rank=card.slice(0,-1)
1316
if (rank=== "A") {
1417
return 11;
@@ -19,7 +22,7 @@ function getCardValue(card) {
1922
if(/^(10|J|Q|K)$/.test(rank)) {
2023
return 10
2124
}
22-
throw new Error("Invalid card");
25+
throw new Error("Invalid card rank");
2326
}
2427

2528
// The line below allows us to load the getCardValue function into tests in other files.
@@ -74,6 +77,7 @@ assertEquals(aceOfSpades1, 11);
7477
// Then it should throw an error indicating "Invalid card rank."
7578

7679

77-
assert.throws(() => getCardValue("L♠"), Error, /Invalid card/);
78-
assert.throws(() => getCardValue(), Error, /Invalid card/);
79-
assert.throws(() => getCardValue(["A♠"]), Error, /Invalid card/);
80+
assert.throws(() => getCardValue("L♠"), Error, /Invalid card rank/);
81+
assert.throws(() => getCardValue(), Error, /Invalid card rank/);
82+
assert.throws(() => getCardValue(["A♠"]), Error, /Invalid card rank/);
83+
assert.throws(() => getCardValue("A2"), Error, /Invalid card/);

0 commit comments

Comments
 (0)