Skip to content

Commit 00515f5

Browse files
committed
Add test cases for negative numerator/denominator and other edge cases; update function to handle them
1 parent 6bcf17a commit 00515f5

File tree

1 file changed

+44
-9
lines changed

1 file changed

+44
-9
lines changed

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

Lines changed: 44 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,23 @@
77
// complete the rest of the tests and cases
88
// write one test at a time, and make it pass, build your solution up methodically
99

10+
11+
1012
function isProperFraction(numerator, denominator) {
11-
if (numerator < denominator && numerator !== 0) {
12-
return true;
13-
} else if (numerator >= denominator) {
14-
return false;
15-
} else if (numerator === 0) {
16-
return false;
13+
let actualOutput;
14+
// Denominator must be positive
15+
if (denominator <= 0) {
16+
actualOutput = false;
17+
}
18+
// Numerator must be positive and smaller than denominator
19+
else if (numerator > 0 && numerator < denominator) {
20+
actualOutput = true;
1721
}
22+
// All other cases are not proper fractions
23+
else {
24+
actualOutput = false;
25+
}
26+
return actualOutput;
1827
}
1928

2029
// The line below allows us to load the isProperFraction function into tests in other files.
@@ -47,11 +56,11 @@ assertEquals(improperFraction, false);
4756

4857
// Negative Fraction check:
4958
// Input: numerator = -4, denominator = 7
50-
// target output: true
51-
// Explanation: The fraction -4/7 is a proper fraction because the absolute value of the numerator (4) is less than the denominator (7). The function should return true.
59+
// target output: false
60+
// Explanation: Negative numerator should not count as proper fraction.
5261
const negativeFraction = isProperFraction(-4, 7);
5362
// ====> complete with your assertion
54-
assertEquals(negativeFraction, true);
63+
assertEquals(negativeFraction, false);
5564

5665
// Equal Numerator and Denominator check:
5766
// Input: numerator = 3, denominator = 3
@@ -63,5 +72,31 @@ assertEquals(equalFraction, false);
6372

6473
// Stretch:
6574
// What other scenarios could you test for?
75+
76+
// Zero Numerator check:
77+
// Input: numerator = 0, denominator = 5
78+
// Target output: false
79+
// Explanation: A fraction with numerator 0 is not considered a proper fraction.
6680
const zeroNumerator = isProperFraction(0, 5);
6781
assertEquals(zeroNumerator, false);
82+
83+
// Negative Denominator check:
84+
// Input: numerator = 3, denominator = -5
85+
// Target output: false
86+
// Explanation: Denominator must be positive for a proper fraction.
87+
const negativeDenominator = isProperFraction(3, -5);
88+
assertEquals(negativeDenominator, false);
89+
90+
// Both Negative check:
91+
// Input: numerator = -2, denominator = -3
92+
// Target output: false
93+
// Explanation: Proper fractions must have positive numerator and denominator.
94+
const bothNegative = isProperFraction(-2, -3);
95+
assertEquals(bothNegative, false);
96+
97+
// Zero Denominator check:
98+
// Input: numerator = 1, denominator = 0
99+
// Target output: false
100+
// Explanation: Division by zero is invalid; fraction cannot be proper.
101+
const zeroDenominator = isProperFraction(1, 0);
102+
assertEquals(zeroDenominator, false);

0 commit comments

Comments
 (0)