Skip to content

Commit bcbd867

Browse files
committed
added a regex to make sure only exact number card value matches will be converted to numbers.
1 parent 50ee9ef commit bcbd867

File tree

1 file changed

+24
-7
lines changed

1 file changed

+24
-7
lines changed

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

Lines changed: 24 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,23 +8,40 @@
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-
let rank = "";
11+
function strictNumber(rank) {
12+
if (/^(0|[1-9][0-9]*)$/.test(rank)) {
13+
return true;
14+
}
15+
return false;
16+
}
17+
18+
let rank = card.slice(0, card.length - 1);
1219

13-
for (let i = 0; i < card.length - 1; i++) {
14-
rank += card[i];
20+
if (strictNumber(rank)) {
21+
if (
22+
Number.isInteger(Number(rank)) &&
23+
Number(rank) > 1 &&
24+
Number(rank) <= 10
25+
) {
26+
return Number(rank);
27+
}
1528
}
1629

17-
if (Number.isInteger(Number(rank)) && Number(rank) > 1 && Number(rank) < 10) {
18-
return Number(rank);
19-
} else if (rank === "10" || rank === "J" || rank === "Q" || rank === "K") {
30+
if (rank.includes("J") || rank.includes("Q") || rank.includes("K")) {
2031
return 10;
21-
} else if (rank === "A") {
32+
} else if (rank.includes("A")) {
2233
return 11;
2334
} else {
2435
return "Invalid card rank.";
2536
}
2637
}
2738

39+
console.log(
40+
`some test values ${getCardValue("09♥")}, ${getCardValue(
41+
"10.0♥"
42+
)}, ${getCardValue("2♥")}, ${getCardValue("3.5♥")} `
43+
);
44+
2845
// The line below allows us to load the getCardValue function into tests in other files.
2946
// This will be useful in the "rewrite tests with jest" step.
3047
module.exports = getCardValue;

0 commit comments

Comments
 (0)