Skip to content

Commit 83a3f77

Browse files
wrote tests to check cards validity and implemented the function to pass the tests
1 parent 35c3083 commit 83a3f77

File tree

1 file changed

+14
-2
lines changed

1 file changed

+14
-2
lines changed

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

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
// complete the rest of the tests and cases
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
10+
const assert = require("assert");
1011
function getCardValue(card) {
1112
let rank=card.slice(0,-1)
1213
if (rank=== "A") {
@@ -15,9 +16,10 @@ function getCardValue(card) {
1516
if(rank>=2 && rank<=9){
1617
return +rank
1718
}
18-
if(rank==="J") {
19+
if(/^(10|J|Q|K)$/.test(rank)) {
1920
return 10
2021
}
22+
throw new Error("Invalid card");
2123
}
2224

2325
// The line below allows us to load the getCardValue function into tests in other files.
@@ -53,15 +55,25 @@ assertEquals(fiveOfHearts,5)
5355
// Given a card with a rank of "10," "J," "Q," or "K",
5456
// When the function is called with such a card,
5557
// Then it should return the value 10, as these cards are worth 10 points each in blackjack.
56-
const faceCards = getCardValue("J♠");
58+
const faceCards = getCardValue("K♠");
5759
assertEquals(faceCards,10)
60+
const faceCards1 = getCardValue("10♠");
61+
assertEquals(faceCards1, 10);
62+
5863

5964
// Handle Ace (A):
6065
// Given a card with a rank of "A",
6166
// When the function is called with an Ace,
6267
// Then it should, by default, assume the Ace is worth 11 points, which is a common rule in blackjack.
68+
const aceOfSpades1 = getCardValue("A♠");
69+
assertEquals(aceOfSpades1, 11);
6370

6471
// Handle Invalid Cards:
6572
// Given a card with an invalid rank (neither a number nor a recognized face card),
6673
// When the function is called with such a card,
6774
// Then it should throw an error indicating "Invalid card rank."
75+
76+
77+
assert.throws(() => getCardValue("L♠"), Error, /Invalid card/);
78+
assert.throws(() => getCardValue(), Error, /Invalid card/);
79+
assert.throws(() => getCardValue(["A♠"]), Error, /Invalid card/);

0 commit comments

Comments
 (0)