Skip to content

Commit b1186bd

Browse files
committed
Implement isProperFraction and add assert and Jest tests"
1 parent 80d0651 commit b1186bd

2 files changed

Lines changed: 47 additions & 5 deletions

File tree

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

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

1313
function isProperFraction(numerator, denominator) {
14-
// TODO: Implement this function
14+
let numeratorVal = Math.abs(numerator);
15+
let denominatorVal = Math.abs(denominator);
16+
17+
if (denominator === 0) return false;
18+
if (numeratorVal < denominatorVal) return true;
19+
return false;
1520
}
1621

1722
// The line below allows us to load the isProperFraction function into tests in other files.
@@ -26,8 +31,18 @@ function assertEquals(actualOutput, targetOutput) {
2631
);
2732
}
2833

34+
assertEquals(isProperFraction(0, 4), true);
35+
assertEquals(isProperFraction(4, 0), false);
36+
assertEquals(isProperFraction(7, 4), false);
37+
assertEquals(isProperFraction(4, 7), true);
38+
assertEquals(isProperFraction(-7, 4), false);
39+
assertEquals(isProperFraction(7, -4), false);
40+
assertEquals(isProperFraction(-4, 7), true);
41+
assertEquals(isProperFraction(4, -7), true);
42+
assertEquals(isProperFraction(4, 4), false);
43+
2944
// TODO: Write tests to cover all cases.
3045
// What combinations of numerators and denominators should you test?
3146

3247
// Example: 1/2 is a proper fraction
33-
assertEquals(isProperFraction(1, 2), true);
48+
//assertEquals(isProperFraction(1, 2), true);

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

Lines changed: 30 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,34 @@ 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

7-
// Special case: numerator is zero
8-
test(`should return false when denominator is zero`, () => {
9-
expect(isProperFraction(1, 0)).toEqual(false);
7+
describe("isProperFraction", () => {
8+
test("returns true when numerator is zero", () => {
9+
expect(isProperFraction(0, 4)).toEqual(true);
10+
});
11+
12+
test("returns false when denominator is zero", () => {
13+
expect(isProperFraction(4, 0)).toEqual(false);
14+
});
15+
16+
test("returns true for proper positive fractions", () => {
17+
expect(isProperFraction(4, 7)).toEqual(true);
18+
});
19+
20+
test("returns false for improper fractions", () => {
21+
expect(isProperFraction(7, 4)).toEqual(false);
22+
});
23+
24+
test("handles proper negative fractions", () => {
25+
expect(isProperFraction(-4, 7)).toEqual(true);
26+
expect(isProperFraction(4, -7)).toEqual(true);
27+
});
28+
29+
test("handles improper negative fractions", () => {
30+
expect(isProperFraction(-7, 4)).toEqual(false);
31+
expect(isProperFraction(7, -4)).toEqual(false);
32+
});
33+
34+
test("returns false when numerator equals denominator", () => {
35+
expect(isProperFraction(4, 4)).toEqual(false);
36+
});
1037
});

0 commit comments

Comments
 (0)