Skip to content

Commit ebe0304

Browse files
committed
Added getCardValue function logic
1 parent 69bef1b commit ebe0304

File tree

2 files changed

+42
-21
lines changed

2 files changed

+42
-21
lines changed

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

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,9 @@
1111
// execute the code to ensure all tests pass.
1212

1313
function isProperFraction(numerator, denominator) {
14-
return numerator < denominator;
14+
if (denominator === 0) return false;
15+
16+
return Math.abs(numerator) < Math.abs(denominator);
1517
}
1618

1719
// The line below allows us to load the isProperFraction function into tests in other files.
@@ -30,18 +32,18 @@ module.exports = isProperFraction;
3032
// What combinations of numerators and denominators should you test?
3133

3234
//Test
33-
// Proper fraction
34-
assertEquals(isProperFraction(3, 5), true);
35-
assertEquals(isProperFraction(4, 8), true);
36-
assertEquals(isProperFraction(-1, 2), true);
35+
// Proper fraction with negatives
36+
assertEquals(isProperFraction(7, 9), true);
37+
assertEquals(isProperFraction(-7, 9), true);
38+
assertEquals(isProperFraction(7, -9), true);
3739
assertEquals(isProperFraction(-1, -2), true);
38-
assertEquals(isProperFraction(1, -2), true);
40+
assertEquals(isProperFraction(0, -5), true);
3941

40-
//Not
41-
assertEquals(isProperFraction(9, 7), false);
42-
assertEquals(isProperFraction(13, 11), false);
43-
assertEquals(isProperFraction(19, 10), false);
42+
// Improper fractions
43+
assertEquals(isProperFraction(9, -7), false);
44+
assertEquals(isProperFraction(-9, 7), false);
45+
assertEquals(isProperFraction(-9, -7), false);
4446
assertEquals(isProperFraction(17, 3), false);
4547

4648
//Edge case
47-
assertEquals(isProperFraction(0, 5), true);
49+
assertEquals(isProperFraction(1, 0), false);

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

Lines changed: 29 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,14 @@
2222
// execute the code to ensure all tests pass.
2323

2424
function getCardValue(card) {
25+
if (!card || typeof card !== "string") {
26+
throw new Error("Invalid card");
27+
}
2528
const rank = card.slice(0, -1);
2629
const suit = card.slice(-1);
2730

28-
const vaildSuits = ["♠", "♥", "♦", "♣"];
29-
const vailRanks = [
31+
const validSuits = ["♠", "♥", "♦", "♣"];
32+
const validRanks = [
3033
"A",
3134
"2",
3235
"3",
@@ -42,7 +45,7 @@ function getCardValue(card) {
4245
"K",
4346
];
4447

45-
if (!vaildSuits.includes(suit) || !vailRanks.includes(rank)) {
48+
if (!validSuits.includes(suit) || !validRanks.includes(rank)) {
4649
throw new Error("Invalid card");
4750
}
4851
if (rank === "A") return 11;
@@ -66,18 +69,34 @@ function assertEquals(actualOutput, targetOutput) {
6669

6770
// TODO: Write tests to cover all outcomes, including throwing errors for invalid cards.
6871
// Examples:
72+
73+
// Valid cards
6974
assertEquals(getCardValue("9♠"), 9);
70-
//Test
7175
assertEquals(getCardValue("A♠"), 11);
7276
assertEquals(getCardValue("J♣"), 10);
7377
assertEquals(getCardValue("10♥"), 10);
7478

75-
// Handling invalid cards
79+
80+
81+
// What other invalid card cases can you think of?
82+
83+
// Invalid cases
7684
try {
7785
getCardValue("invalid");
86+
console.log("fail invalid");
87+
} catch (e) {
88+
console.log("pass invalid");
89+
}
7890

79-
// This line will not be reached if an error is thrown as expected
80-
console.error("Error was not thrown for invalid card");
81-
} catch (e) {}
82-
83-
// What other invalid card cases can you think of?
91+
try {
92+
getCardValue("1♠");
93+
console.log("fail 1♠");
94+
} catch (e) {
95+
console.log("pass 1♠");
96+
}
97+
try {
98+
getCardValue("A");
99+
console.log("fail A");
100+
} catch (e) {
101+
console.log("pass A");
102+
}

0 commit comments

Comments
 (0)