Skip to content

Commit 7e6166b

Browse files
Enhance getCardValue function comments and add error handling for invalid card ranks
1 parent 801d5d8 commit 7e6166b

File tree

1 file changed

+11
-2
lines changed

1 file changed

+11
-2
lines changed

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

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
function getCardValue(card) {
1111
var rank = card.slice(0, -1); // get the rank of the card by removing the last character. (the suit is the last character)
1212
if (rank === "A") return 11; // this checks for Aces
13+
// Handle Number Cards (2-9)
1314
if (rank === "2") return 2; // this checks for the twos
1415
if (rank === "3") return 3; // this checks for the threes
1516
if (rank === "4") return 4; // this checks for the fours
@@ -18,10 +19,13 @@ function getCardValue(card) {
1819
if (rank === "7") return 7; // this checks for the sevens
1920
if (rank === "8") return 8; // this checks for the eights
2021
if (rank === "9") return 9; // this checks for the nines
22+
// Handle Face Cards (J, Q, K) And 10's
2123
if (rank === "J") return 10; // this checks for Jacks
2224
if (rank === "Q") return 10; // this checks for Queens
2325
if (rank === "K") return 10; // this checks for Kings
2426
if (rank === "10") return 10; // this checks for Tens
27+
// if none of the above its an invalid card and throw an error
28+
throw new Error("Invalid card rank."); // this will throw an error if the card is not a valid rank
2529
}
2630
// You need to write assertions for your function to check it works in different cases
2731
// we're going to use this helper function to make our assertions easier to read
@@ -44,6 +48,7 @@ assertEquals(aceofSpades, 11);
4448
// Given a card with a rank between "2" and "9",
4549
// When the function is called with such a card,
4650
// Then it should return the numeric value corresponding to the rank (e.g., "5" should return 5).
51+
// ====> write your test here, and then add a line to pass the test in the function above
4752
const fiveofHearts = getCardValue("5♥");
4853
const sixofDiamonds = getCardValue("6♦");
4954
const sevenofClubs = getCardValue("7♣");
@@ -52,7 +57,6 @@ assertEquals(fiveofHearts, 5);
5257
assertEquals(sixofDiamonds, 6);
5358
assertEquals(sevenofClubs, 7);
5459
assertEquals(eightofSpades, 8);
55-
// ====> write your test here, and then add a line to pass the test in the function above
5660

5761
// Handle Face Cards (J, Q, K):
5862
// Given a card with a rank of "10," "J," "Q," or "K",
@@ -72,4 +76,9 @@ assertEquals(kingOfSpades, 10);
7276
// Given a card with an invalid rank (neither a number nor a recognized face card),
7377
// When the function is called with such a card,
7478
// Then it should throw an error indicating "Invalid card rank."
75-
79+
try {
80+
getCardValue("z♠"); // this should throw an error of "Invalid card rank."
81+
console.log("Test failed: Expected an error for invalid card rank.");
82+
} catch(error){
83+
assertEquals(error.meessage, "Invalid card rank.");
84+
}

0 commit comments

Comments
 (0)