Skip to content

Commit e8fd53d

Browse files
committed
Improve getCardValue() to handle the parameter as well as validating the rank when it comes to handling number cards
1 parent 3f52c42 commit e8fd53d

File tree

1 file changed

+10
-2
lines changed

1 file changed

+10
-2
lines changed

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

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,13 @@
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);
11+
const rank = card.length > 1 ? card.slice(0, -1) : card;
1212

1313
if (rank === "A") {
1414
return 11;
1515
} else if (["J", "Q", "K"].includes(rank)) {
1616
return 10;
17-
} else if (rank >= 2 && rank <= 10) {
17+
} else if (/^(?:[2-9]|10)$/.test(rank)) {
1818
return Number(rank);
1919
} else {
2020
return new Error("Invalid card rank.");
@@ -67,5 +67,13 @@ assertEquals(ace, 11);
6767
// Given a card with an invalid rank (neither a number nor a recognized face card),
6868
// When the function is called with such a card,
6969
// Then it should throw an error indicating "Invalid card rank."
70+
71+
// assertEquals() doesn’t handle an actual Error being thrown, so the console will display:
72+
//
73+
// Expected Error: Invalid card rank. to equal Invalid card rank.
74+
// 3-get-card-value.js:32
75+
// Assertion failed: Expected Error: Invalid card rank. to equal Invalid card rank.
76+
//
77+
// It will work if getCardValue() returns a string instead.
7078
const invalidRank = getCardValue("@");
7179
assertEquals(invalidRank, "Invalid card rank.");

0 commit comments

Comments
 (0)