Skip to content

Commit 4541230

Browse files
committed
added implementation for invalid cards and removed direct calls from get-value-card to test file
1 parent 5218dc7 commit 4541230

File tree

2 files changed

+27
-12
lines changed

2 files changed

+27
-12
lines changed

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

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -22,15 +22,25 @@
2222
// execute the code to ensure all tests pass.
2323

2424
function getCardValue(card) {
25-
// TODO: Implement this function
26-
const rank = card.slice(0, -1);
27-
const suit = card.slice(-1);
28-
if (rank === "A") {
29-
return 11;
25+
const validRanks = ["A","2","3","4","5","6","7","8","9","10","J","Q","K"];
26+
const validSuits = ["♠","♥","♦","♣"];
27+
28+
// Check type
29+
if (typeof card !== "string") {
30+
throw new Error("Invalid card string");
3031
}
31-
if (rank === "J" || rank === "Q" || rank === "K") {
32-
return 10;
32+
33+
const suit = card.slice(-1);
34+
const rank = card.slice(0, -1);
35+
36+
// Validate rank and suit
37+
if (!validRanks.includes(rank) || !validSuits.includes(suit)) {
38+
throw new Error("Invalid card string");
3339
}
40+
41+
// Compute value
42+
if (rank === "A") return 11;
43+
if (["J","Q","K"].includes(rank)) return 10;
3444
return parseInt(rank);
3545
}
3646

@@ -48,7 +58,7 @@ function assertEquals(actualOutput, targetOutput) {
4858

4959
// TODO: Write tests to cover all outcomes, including throwing errors for invalid cards.
5060
// Examples:
51-
assertEquals(getCardValue("9♠"), 9);
61+
/* assertEquals(getCardValue("9♠"), 9);
5262
5363
// Handling invalid cards
5464
try {
@@ -68,3 +78,4 @@ try {
6878
try { getCardValue("9X"); // Invalid suit
6979
console.error("Error was not thrown for invalid suit");
7080
} catch (e) {}
81+
*/

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

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,13 +23,17 @@ test(`Should return 10 for face cards`, () => {
2323
});
2424
// Invalid Cards
2525
test(`Should throw an error for invalid cards`, () => {
26-
expect(() => getCardValue("invalid")).toThrow();
27-
expect(() => getCardValue("A")).toThrow(); // Missing suit
28-
expect(() => getCardValue("11♠")).toThrow(); // Invalid rank
29-
expect(() => getCardValue("9X")).toThrow(); // Invalid suit
26+
expect(() => getCardValue("invalid")).toThrow("Invalid card string");
27+
expect(() => getCardValue("A")).toThrow("Invalid card string"); // Missing suit
28+
expect(() => getCardValue("11♠")).toThrow("Invalid card string"); // Invalid rank
29+
expect(() => getCardValue("9X")).toThrow("Invalid card string"); // Invalid suit
3030
});
3131

3232
// To learn how to test whether a function throws an error as expected in Jest,
3333
// please refer to the Jest documentation:
3434
// https://jestjs.io/docs/expect#tothrowerror
3535

36+
37+
38+
39+

0 commit comments

Comments
 (0)