Skip to content

Commit f7e112c

Browse files
committed
Fix isProperFraction implementation and update tests
1 parent 32baac9 commit f7e112c

File tree

2 files changed

+64
-23
lines changed

2 files changed

+64
-23
lines changed
Lines changed: 30 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,59 +1,70 @@
1-
// Implement a function isProperFraction,
2-
// when given two numbers, a numerator and a denominator, it should return true if
3-
// the given numbers form a proper fraction, and false otherwise.
1+
// Implement a function isProperFraction.
2+
// When given two numbers, a numerator and a denominator, it should return true
3+
// if the given numbers form a proper fraction, and false otherwise.
44

55
// 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+
// Definition:
8+
// A proper fraction is a fraction where:
9+
// - the denominator is not zero
10+
// - both numbers are non-negative
11+
// - the numerator is smaller than the denominator
812

913
// Acceptance criteria:
10-
// After you have implemented the function, write tests to cover all the cases, and
11-
// execute the code to ensure all tests pass.
14+
// After implementing the function, write tests to cover all cases
15+
// and run the code to ensure all tests pass.
1216

1317
function isProperFraction(numerator, denominator) {
1418
// A fraction with denominator 0 is invalid
1519
if (denominator === 0) {
1620
return false;
1721
}
1822

19-
// A proper fraction has absolute numerator smaller than absolute denominator
20-
if (Math.abs(numerator) < Math.abs(denominator)) {
23+
// Negative values are not allowed
24+
if (numerator < 0 || denominator < 0) {
25+
return false;
26+
}
27+
28+
// A proper fraction must have numerator smaller than denominator
29+
if (numerator < denominator) {
2130
return true;
2231
}
2332

33+
// All other cases are not proper fractions
2434
return false;
2535
}
2636

2737
// The line below allows us to load the isProperFraction function into tests in other files.
2838
// This will be useful in the "rewrite tests with jest" step.
2939
module.exports = isProperFraction;
3040

31-
// Here's our helper again
41+
// Helper function for simple assertions in this file
3242
function assertEquals(actualOutput, targetOutput) {
3343
console.assert(
3444
actualOutput === targetOutput,
3545
`Expected ${actualOutput} to equal ${targetOutput}`
3646
);
3747
}
3848

39-
// TODO: Write tests to cover all cases.
40-
// What combinations of numerators and denominators should you test?
49+
// Tests to cover different combinations of numerators and denominators
4150

4251
// Example: 1/2 is a proper fraction
4352
assertEquals(isProperFraction(1, 2), true);
4453

45-
// Proper fractions
54+
// Proper fractions (numerator smaller than denominator)
4655
assertEquals(isProperFraction(3, 5), true);
47-
assertEquals(isProperFraction(-2, 7), true);
48-
assertEquals(isProperFraction(2, -7), true);
56+
assertEquals(isProperFraction(2, 7), true);
4957

50-
// Improper fractions
58+
// Improper fractions (numerator greater than or equal to denominator)
5159
assertEquals(isProperFraction(5, 5), false);
5260
assertEquals(isProperFraction(7, 3), false);
53-
assertEquals(isProperFraction(-8, 4), false);
5461

55-
// Zero numerator
62+
// Negative numbers should return false
63+
assertEquals(isProperFraction(-2, 7), false);
64+
assertEquals(isProperFraction(2, -7), false);
65+
66+
// Zero numerator is allowed if denominator is positive
5667
assertEquals(isProperFraction(0, 5), true);
5768

58-
// Invalid fraction (denominator 0)
59-
assertEquals(isProperFraction(2, 0), false);
69+
// Invalid fraction (denominator is zero)
70+
assertEquals(isProperFraction(2, 0), false);

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

Lines changed: 34 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,39 @@
22
// We will use the same function, but write tests for it using Jest in this file.
33
const isProperFraction = require("../implement/2-is-proper-fraction");
44

5-
// TODO: Write tests in Jest syntax to cover all combinations of positives, negatives, zeros, and other categories.
5+
// These tests cover different combinations of values such as:
6+
// positive numbers, negative numbers, zeros, and improper fractions.
67

7-
// Special case: numerator is zero
8-
test(`should return false when denominator is zero`, () => {
9-
expect(isProperFraction(1, 0)).toEqual(false);
8+
describe("isProperFraction", () => {
9+
// Special case: denominator is zero
10+
test("should return false when denominator is zero", () => {
11+
expect(isProperFraction(1, 0)).toEqual(false);
12+
});
13+
14+
// Special case: numerator is zero
15+
test("should return true when numerator is zero and denominator is positive", () => {
16+
expect(isProperFraction(0, 5)).toEqual(true);
17+
});
18+
19+
// Proper fraction: numerator is positive and less than denominator
20+
test("should return true for a proper fraction", () => {
21+
expect(isProperFraction(1, 2)).toEqual(true);
22+
expect(isProperFraction(3, 4)).toEqual(true);
23+
});
24+
25+
// Improper fraction: numerator is greater than or equal to denominator
26+
test("should return false for an improper fraction", () => {
27+
expect(isProperFraction(5, 4)).toEqual(false);
28+
expect(isProperFraction(4, 4)).toEqual(false);
29+
});
30+
31+
// Negative numerator
32+
test("should return false when numerator is negative", () => {
33+
expect(isProperFraction(-1, 2)).toEqual(false);
34+
});
35+
36+
// Negative denominator
37+
test("should return false when denominator is negative", () => {
38+
expect(isProperFraction(1, -2)).toEqual(false);
39+
});
1040
});

0 commit comments

Comments
 (0)