Skip to content

Commit 2a7ca83

Browse files
added extra invalid tests, changed if statements
1 parent 767fe59 commit 2a7ca83

File tree

1 file changed

+25
-27
lines changed

1 file changed

+25
-27
lines changed

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

Lines changed: 25 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,11 @@ function getCardValue(card) {
2525
// if card is less than 2 characters or more than 3 characters it is invalid
2626
// and check if not a string
2727

28-
if (typeof card !== "string" || card.length < 2 || card.length > 3) {
28+
if (typeof card !== "string") {
29+
throw new Error("Invalid card");
30+
}
31+
32+
if (!(card.length === 2 || (card.length === 3 && card.startsWith("10")))) {
2933
throw new Error("Invalid card");
3034
}
3135

@@ -49,7 +53,7 @@ function getCardValue(card) {
4953
if (["J", "Q", "K"].includes(firstChar)) return 10; // if Jack, Queen or King return 10
5054

5155
const num = Number(firstChar);
52-
if (num >= 2 && num <= 10) return num; //checks number is between 2 and 10
56+
if (!isNaN(num) && num >= 2 && num <= 10) return num; //checks number is between 2 and 10
5357

5458
// for everything else that is invalid
5559
throw new Error("Invalid number");
@@ -74,13 +78,15 @@ assertEquals(getCardValue("10♥"), 10);
7478
assertEquals(getCardValue("J♥"), 10);
7579
assertEquals(getCardValue("A♠"), 11);
7680
assertEquals(getCardValue("Q♦"), 10);
77-
assertEquals(getCardValue("K"), 10);
81+
assertEquals(getCardValue("K"), 10);
7882

7983
// Handling invalid cards
8084
try {
8185
getCardValue("♠J");
82-
console.error("Error was not thrown for invalid suit first");
83-
} catch (e) {}
86+
console.error("Test failed: invalid first character accepted");
87+
} catch (e) {
88+
console.log("Test passed: Invalid first character rejected");
89+
}
8490

8591
try {
8692
getCardValue("invalid");
@@ -90,29 +96,21 @@ try {
9096
}
9197

9298
try {
93-
getCardValue("22");
94-
console.error("Error was not thrown for invalid number");
95-
} catch (e) {}
99+
console.log(getCardValue("2♥2"));
100+
} catch (e) {
101+
console.log("Invalid numbers caught");
102+
}
103+
104+
try {
105+
console.log(getCardValue("2♥♥"));
106+
} catch (e) {
107+
console.log("Invalid suit caught");
108+
}
96109

97-
console.log(getCardValue("9♠"));
98-
console.log(getCardValue("10♥"));
99-
console.log(getCardValue("J♥"));
110+
console.log(getCardValue("A♠"));
111+
console.log(getCardValue("A♠"));
112+
console.log(getCardValue("A♠"));
100113
console.log(getCardValue("A♠"));
101114
console.log(getCardValue("Q♦"));
102115
console.log(getCardValue("K♣"));
103-
104-
// What other invalid card cases can you think of?
105-
106-
// There could be cards with special characters.
107-
108-
// There could be cards with two numbers rather than a number and a suit
109-
// These will not be picked up because the code only checks for if starts with 10 or if the first character
110-
// is a number between 2 and 9, so cards like "22" would be valid because the first number
111-
// is 2, but the second character is not checked for validity. It is also 2 characters
112-
// so will not cause an error when the length is checked.
113-
114-
// Since the second character is not checked it could be 2D which is not a valid card but
115-
// would be accepted because the first character is 2 and the second character is not checked for validity
116-
117-
// When the card is checked if it begins with 10 it does not check if it has a valid suit
118-
// as only the first 2 characters are checked so it could be 10DEVON or 10♥♥.
116+
console.log(getCardValue("A♠"));

0 commit comments

Comments
 (0)