Skip to content

Commit 3d7735d

Browse files
committed
Implement isProperFraction and add tests
1 parent 1f53f87 commit 3d7735d

2 files changed

Lines changed: 31 additions & 10 deletions

File tree

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,21 @@
11
// Implement a function isProperFraction,
2-
// when given two numbers, a numerator and a denominator, it should return true if
2+
// when given two numbers, a numerator and a denominator, returns true if
33
// the given numbers form a proper fraction, and false otherwise.
44

5-
// Assumption: The parameters are valid numbers (not NaN or Infinity).
5+
// Assumption: The parameters are valid numbers (not NaN or infinity).
66

7-
// Note: If you are unfamiliar with proper fractions, please look up its mathematical definition.
7+
// Note: If you are unfamiliar with proper fractions, please look it up.
88

99
// Acceptance criteria:
10-
// After you have implemented the function, write tests to cover all the cases, and
10+
// After you have implemented the function, write tests to
1111
// execute the code to ensure all tests pass.
1212

1313
function isProperFraction(numerator, denominator) {
14-
// TODO: Implement this function
14+
return Math.abs(numerator) < Math.abs(denominator);
1515
}
1616

17-
// The line below allows us to load the isProperFraction function into tests in other files.
18-
// This will be useful in the "rewrite tests with jest" step.
17+
// The line below allows us to load the isProperFraction function into other files
18+
// This will be useful in the "rewrite tests with jest" step later
1919
module.exports = isProperFraction;
2020

2121
// Here's our helper again
@@ -27,7 +27,11 @@ function assertEquals(actualOutput, targetOutput) {
2727
}
2828

2929
// TODO: Write tests to cover all cases.
30-
// What combinations of numerators and denominators should you test?
30+
// What combinations of numerators and denominators should be tested?
3131

3232
// Example: 1/2 is a proper fraction
3333
assertEquals(isProperFraction(1, 2), true);
34+
assertEquals(isProperFraction(2, 1), false);
35+
assertEquals(isProperFraction(-1, 2), true);
36+
assertEquals(isProperFraction(3, 3), false);
37+

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

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,24 @@ 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`, () => {
7+
// Special case: denominator is zero
8+
test('should return false when denominator is zero', () => {
99
expect(isProperFraction(1, 0)).toEqual(false);
1010
});
11+
12+
// Case: proper fractions
13+
test('should return true when the fraction is proper', () => {
14+
expect(isProperFraction(1, 2)).toEqual(true);
15+
expect(isProperFraction(-1, 2)).toEqual(true);
16+
});
17+
18+
// Case: improper fractions
19+
test('should return false when the fraction is improper', () => {
20+
expect(isProperFraction(2, 1)).toEqual(false);
21+
expect(isProperFraction(3, 3)).toEqual(false);
22+
});
23+
24+
// Case: numerator is zero
25+
test('should return true when numerator is zero', () => {
26+
expect(isProperFraction(0, 5)).toEqual(true);
27+
});

0 commit comments

Comments
 (0)