Skip to content

Commit 9f67b6c

Browse files
committed
fixed the error and add throw new error if the denominator is 0 and added test as well
1 parent ec69eea commit 9f67b6c

File tree

2 files changed

+15
-10
lines changed

2 files changed

+15
-10
lines changed

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@
1212

1313
function isProperFraction(numerator, denominator) {
1414
// TODO: Implement this function
15-
if (Math.abs(numerator) < Math.abs(denominator)) return true;
16-
else return false;
15+
if (denominator === 0) throw new Error("Denominator cannot be zero");
16+
return Math.abs(numerator) < Math.abs(denominator);
1717
}
1818
// The line below allows us to load the isProperFraction function into tests in other files.
1919
// This will be useful in the "rewrite tests with jest" step.

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

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,24 +4,29 @@ const isProperFraction = require("../implement/2-is-proper-fraction");
44

55
// TODO: Write tests in Jest syntax to cover all combinations of positives, negatives, zeros, and other categories.
66
// Special case: numerator is zero
7-
test(`should return false when denominator is zero`, () => {
8-
expect(isProperFraction(-11, 0)).toEqual(false);
9-
expect(isProperFraction(0, 0)).toEqual(false);
10-
expect(isProperFraction(7, 0)).toEqual(false);
7+
test("should Throw new error for abs(denominator) == 0", () => {
8+
expect(() => isProperFraction(-11, 0)).toThrow("Denominator cannot be zero");
9+
expect(() => isProperFraction(0, 0)).toThrow("Denominator cannot be zero");
10+
expect(() => isProperFraction(7, 0)).toThrow("Denominator cannot be zero");
1111
});
1212
// It should return false when the numerator > the denominator
13-
test("should return true when numerator is 0", () => {
13+
test("should return false when abs(numerator) == 0", () => {
1414
expect(isProperFraction(0, -1)).toEqual(true);
1515
expect(isProperFraction(0, 3)).toEqual(true);
1616
expect(isProperFraction(0, -2)).toEqual(true);
1717
});
18-
test("should return true when abs(numerator) > abs(denominator)", () => {
18+
test("should return true when abs(numerator) < abs(denominator)", () => {
1919
expect(isProperFraction(-12, -19)).toEqual(true);
20-
expect(isProperFraction(0, 6)).toEqual(true);
20+
expect(isProperFraction(3, 6)).toEqual(true);
2121
expect(isProperFraction(17, -71)).toEqual(true);
2222
});
23-
test("should return false when abs(numerator) < abs(denominator)", () => {
23+
test("should return false when abs(numerator) > abs(denominator)", () => {
2424
expect(isProperFraction(-180, -109)).toEqual(false);
2525
expect(isProperFraction(27, 5)).toEqual(false);
2626
expect(isProperFraction(-29, 17)).toEqual(false);
2727
});
28+
test("should return false when abs(numerator) == abs(denominator)", () => {
29+
expect(isProperFraction(9, 9)).toEqual(false);
30+
expect(isProperFraction(-17, -17)).toEqual(false);
31+
expect(isProperFraction(-15, 15)).toEqual(false);
32+
});

0 commit comments

Comments
 (0)