Skip to content

Commit 8ebeba7

Browse files
committed
test: implement validation checks for card value and suit in getCardValue function
1 parent 468706d commit 8ebeba7

File tree

1 file changed

+52
-3
lines changed

1 file changed

+52
-3
lines changed

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

Lines changed: 52 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,50 @@
2222
// execute the code to ensure all tests pass.
2323

2424
function getCardValue(card) {
25-
// TODO: Implement this function
25+
//check the length and if does not match the valid length throw an error
26+
if (card.length < 2 || card.length > 3) {
27+
throw new Error("Invalid card");
28+
} "♠";
29+
const suits = ["♠", "♥", "♦", "♣"];
30+
const suit = card[card.length - 1];
31+
32+
let suitValid = false;
33+
34+
for(let i = 0; i < suits.length; i++){
35+
if(suit === suits[i]){
36+
suitValid = true;
37+
break;
38+
}
39+
}
40+
// after comparing each suit in our array and does not match throw an error
41+
if(suitValid === false){
42+
throw new Error("Invalid card suit");
43+
}
44+
45+
46+
let rank = card.substring(0, card.length -1);
47+
console.log(rank);
48+
49+
if (rank === "A") {
50+
return 11;
51+
}
52+
if (rank === "J" || rank === "Q" || rank === "K") {
53+
return 10;
54+
}
55+
56+
let number = Number(rank);
57+
58+
if (isNaN(number) || number < 2 || number > 10) {
59+
throw new Error("Invalid card rank");
60+
}
61+
62+
return number;
2663
}
2764

65+
66+
67+
68+
2869
// The line below allows us to load the getCardValue function into tests in other files.
2970
// This will be useful in the "rewrite tests with jest" step.
3071
module.exports = getCardValue;
@@ -39,14 +80,22 @@ function assertEquals(actualOutput, targetOutput) {
3980

4081
// TODO: Write tests to cover all outcomes, including throwing errors for invalid cards.
4182
// Examples:
42-
assertEquals(getCardValue("9♠"), 9);
83+
assertEquals(getCardValue("2♠"), 2), true;
84+
assertEquals(getCardValue("d"), d), false;
85+
// assertEquals(getCardValue("10♥"), 10);
86+
87+
88+
89+
4390

4491
// Handling invalid cards
4592
try {
4693
getCardValue("invalid");
4794

4895
// This line will not be reached if an error is thrown as expected
4996
console.error("Error was not thrown for invalid card");
50-
} catch (e) {}
97+
} catch (e) {
98+
console.log("✓ Error thrown as expected for invalid card");
99+
}
51100

52101
// What other invalid card cases can you think of?

0 commit comments

Comments
 (0)