Skip to content

Commit 72a8a7d

Browse files
committed
Implement getCardValue function and added tests for valid and invalid card cases
1 parent bec514f commit 72a8a7d

File tree

2 files changed

+56
-12
lines changed

2 files changed

+56
-12
lines changed

Sprint-3/1-implement-and-rewrite-tests/implement/2-is-proper-fraction.js

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -40,25 +40,25 @@ function assertEquals(actualOutput, targetOutput) {
4040
assertEquals(isProperFraction(1, 2), true);
4141

4242
// Example: 3/5 is a proper fraction
43-
assertEquals(properFraction(3 / 5), true);
43+
assertEquals(isProperFraction(3, 5), true);
4444

45-
// Example: 1/100 is a positive fraction
46-
assertEquals(smallFraction(1, 100), true);
45+
// Example: 1/100 is a proper fraction
46+
assertEquals(isProperFraction(1, 100), true);
4747

48-
// Example: 0/4 is a proper fraction with zero numerator
49-
assertEquals(zeroNumerator(0, 4), true);
48+
// Example: 5/2 is not a proper fraction
49+
assertEquals(isProperFraction(5, 2), false);
5050

51-
// Example: 7/7 is an improper fraction,
52-
assertEquals(equalFraction(7, 7), false);
51+
// Example: 7/7 is an improper fraction
52+
assertEquals(isProperFraction(7, 7), false);
5353

54-
// Example: 8/3 is an improper fraction,
55-
assertEquals(numeratorLarger(8, 3), false);
54+
// Example: 8/3 is an improper fraction
55+
assertEquals(isProperFraction(8, 3), false);
5656

5757
// Example: 5/0 is an invalid fraction
58-
assertEquals(zeroDenominator(5, 0), false);
58+
assertEquals(isProperFraction(5, 0), false);
5959

6060
// Example: 2/-6 is an invalid fraction
61-
assertEquals(negativeDenominator(2, -6), false);
61+
assertEquals(isProperFraction(2, -6), false);
6262

6363
// Example: -1/4 is an invalid fraction
64-
assertEquals(negativeNumerator(-1, 4), false);
64+
assertEquals(isProperFraction(-1, 4), false);

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

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,14 @@
2323

2424
function getCardValue(card) {
2525
// TODO: Implement this function
26+
if (!["♠", "♣", "♦", "♥"].includes(card.slice(-1)))
27+
throw new Error("Invalid card rank.");
28+
const rank = card.slice(0, card.length - 1);
29+
if (rank === "A") return 11;
30+
if (["10", "J", "Q", "K"].includes(rank)) return 10;
31+
if (["2", "3", "4", "5", "6", "7", "8", "9"].includes(rank))
32+
return Number(rank);
33+
throw new Error("Invalid card rank.");
2634
}
2735

2836
// The line below allows us to load the getCardValue function into tests in other files.
@@ -41,6 +49,12 @@ function assertEquals(actualOutput, targetOutput) {
4149
// Examples:
4250
assertEquals(getCardValue("9♠"), 9);
4351

52+
assertEquals(getCardValue("6♥"), 6);
53+
54+
assertEquals(getCardValue("J♣"), 10);
55+
56+
assertEquals(getCardValue("A♦"), 11);
57+
4458
// Handling invalid cards
4559
try {
4660
getCardValue("invalid");
@@ -50,3 +64,33 @@ try {
5064
} catch (e) {}
5165

5266
// What other invalid card cases can you think of?
67+
68+
try {
69+
getCardValue("9K");
70+
71+
console.log("Error was not thrown for invalid card");
72+
} catch (error) {}
73+
74+
try {
75+
getCardValue("");
76+
77+
console.log("Error was not thrown for invalid card");
78+
} catch (error) {}
79+
80+
try {
81+
getCardValue("ABC");
82+
83+
console.log("Error was not thrown for invalid card");
84+
} catch (error) {}
85+
86+
try {
87+
getCardValue("A");
88+
89+
console.log("Error was not thrown for invalid card");
90+
} catch (error) {}
91+
92+
try {
93+
getCardValue("JK");
94+
95+
console.log("Error was not thrown for invalid card");
96+
} catch (error) {}

0 commit comments

Comments
 (0)