Skip to content

Commit 6874b2e

Browse files
improve variable names and card logic, add tests
1 parent c47b556 commit 6874b2e

File tree

4 files changed

+38
-13
lines changed

4 files changed

+38
-13
lines changed

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

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

1313
function isProperFraction(numerator, denominator) {
14-
return Math.abs(numerator) < Math.abs(denominator)
14+
if (denominator === 0) {
15+
return false;
16+
}
17+
else {
18+
return Math.abs(numerator) < Math.abs(denominator);
19+
}
1520
}
16-
21+
``
1722
// The line below allows us to load the isProperFraction function into tests in other files.
1823
// This will be useful in the "rewrite tests with jest" step.
1924
module.exports = isProperFraction;
@@ -37,4 +42,7 @@ assertEquals(isProperFraction(-1, -2), true);
3742
assertEquals(isProperFraction(-2, -1), false);
3843
assertEquals(isProperFraction(-2, 5), true);
3944
assertEquals(isProperFraction(5, -2), false);
40-
assertEquals(isProperFraction(5, 5), false);
45+
assertEquals(isProperFraction(5, 5), false);
46+
assertEquals(isProperFraction(5, 0), false);
47+
assertEquals(isProperFraction(-2, 0), false);
48+
``

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

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,8 @@
2323

2424
function getCardValue(card) {
2525
const cardNum = card.slice(0, -1)
26-
if (cardNum === "A") { return 11}
26+
const cardSuit = card.slice(-1)
27+
if (cardNum === "A" && "♠♥♦♣".includes(cardSuit)) { return 11}
2728
else if (["J", "Q", "K"].includes(cardNum)) { return 10}
2829
else if (Number(cardNum) >= 2 && Number(cardNum) <= 10) {return Number(cardNum)}
2930
else throw new Error("Invalid card");
@@ -67,4 +68,11 @@ try {
6768
// This line will not be reached if an error is thrown as expected
6869
console.error("Error was not thrown for invalid card");
6970
} catch (e) {}
71+
72+
try {
73+
getCardValue("AX");
74+
75+
// This line will not be reached if an error is thrown as expected
76+
console.error("Error was not thrown for invalid card");
77+
} catch (e) {}
7078
// What other invalid card cases can you think of?

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

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,30 +5,36 @@ const isProperFraction = require("../implement/2-is-proper-fraction");
55
// TODO: Write tests in Jest syntax to cover all combinations of positives, negatives, zeros, and other categories.
66

77
// Special case: numerator is zero
8-
test(`should return false when denominator is zero`, () => {
8+
test(`should return false when denominator is 0`, () => {
99
expect(isProperFraction(1, 0)).toEqual(false);
1010
});
11-
test(`1/2`, () => {
11+
test(`should return true when numerator is less than denominator`, () => {
1212
expect(isProperFraction(1, 2)).toEqual(true);
1313
});
14-
test(`7/1`, () => {
14+
test(`should return false when numerator is greater than denominator`, () => {
1515
expect(isProperFraction(7, 1)).toEqual(false);
1616
});
17-
test(`0/5`, () => {
17+
test(`should return true when numerator is 0`, () => {
1818
expect(isProperFraction(0, 5)).toEqual(true);
1919
});
20-
test(`-1/-2`, () => {
20+
test(`should return true when numerator and denominator are negative`, () => {
2121
expect(isProperFraction(-1, -2)).toEqual(true);
2222
});
23-
test(`-2/-1`, () => {
23+
test(`should return false when numerator and denominator are negative and numerator is greater than denominator`, () => {
2424
expect(isProperFraction(-2, -1)).toEqual(false);
2525
});
26-
test(`-2/5`, () => {
26+
test(`should return true when numerator is negative and denominator is positive`, () => {
2727
expect(isProperFraction(-2, 5)).toEqual(true);
2828
});
29-
test(`5/-2`, () => {
29+
test(`should return false when numerator is positive and denominator is negative`, () => {
3030
expect(isProperFraction(5, -2)).toEqual(false);
3131
});
32-
test(`5/5`, () => {
32+
test(`should return false when numerator equals denominator`, () => {
3333
expect(isProperFraction(5, 5)).toEqual(false);
3434
});
35+
test(`should return false when denominator is 0`, () => {
36+
expect(isProperFraction(5, 0)).toEqual(false);
37+
});
38+
test(`should return false when denominator is 0`, () => {
39+
expect(isProperFraction(-2, 0)).toEqual(false);
40+
});

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,9 @@ test(`Should return Error when given an ♦Q`, () => {
2626
test(`Should return Error when given an 11♦`, () => {
2727
expect(function() {getCardValue("11♦")}).toThrow("Invalid card");
2828
});
29+
test(`Should return Error when given an AX`, () => {
30+
expect(function() {getCardValue("11♦")}).toThrow("Invalid card");
31+
});
2932

3033

3134
// Suggestion: Group the remaining test data into these categories:

0 commit comments

Comments
 (0)