Skip to content

Commit 9abc5a9

Browse files
committed
2-is-proper-fraction.js Updated the function and has been able to handle possible edge cases.
1 parent 0a1ed8c commit 9abc5a9

File tree

1 file changed

+20
-16
lines changed

1 file changed

+20
-16
lines changed

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

Lines changed: 20 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -8,23 +8,14 @@
88
// write one test at a time, and make it pass, build your solution up methodically
99

1010
function isProperFraction(numerator, denominator) {
11-
if (numerator < denominator) {
12-
return true;
13-
}
14-
else if (numerator > denominator){
15-
return false;
16-
}
17-
else if (numerator === denominator) {
11+
// Make sure denominator cannot be zero
12+
if (denominator === 0) {
1813
return false;
1914
}
20-
else if (numerator === 0){
21-
return (true);
22-
}
23-
else if (denominator === 0){
24-
return (false);
25-
}
26-
}
2715

16+
// Check if the absolute value of the numerator is smaller than denominator
17+
return Math.abs(numerator) < Math.abs(denominator);
18+
}
2819
// The line below allows us to load the isProperFraction function into tests in other files.
2920
// This will be useful in the "rewrite tests with jest" step.
3021
module.exports = isProperFraction;
@@ -57,8 +48,8 @@ assertEquals(improperFraction, false);
5748
// Input: numerator = -4, denominator = 7
5849
// target output: true
5950
// 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.
60-
const negativeFraction = isProperFraction(-4, 7);
61-
assertEquals(negativeFraction, true);
51+
const negativeFraction = isProperFraction(-4, 3);
52+
assertEquals(negativeFraction, false);
6253

6354
// Equal Numerator and Denominator check:
6455
// Input: numerator = 3, denominator = 3
@@ -77,5 +68,18 @@ assertEquals(numeratorZero, true);
7768
const denominatorrZero = isProperFraction(5, 0);
7869
assertEquals(denominatorrZero, false);
7970

71+
// Expected: true (|-2| < |5|)
72+
const reviewerCase2 = isProperFraction(-2, 5);
73+
assertEquals(reviewerCase2, true);
74+
75+
// Expected: false (|-1| = |1|)
76+
const reviewerCase3 = isProperFraction(-1, 1);
77+
assertEquals(reviewerCase3, false);
78+
79+
// Expected: true (|-2| < |-3|)
80+
const reviewerCase4 = isProperFraction(-2, -3);
81+
assertEquals(reviewerCase4, true);
82+
8083
// Sprint-3-implement 1-is-proper-fraction.js all assertions and necessary functions added
84+
// Updated the function and has been able to handle possible edge cases.
8185

0 commit comments

Comments
 (0)