Skip to content

Commit 7a4e526

Browse files
committed
Refactor isProperFraction to improve readability and reduce repetition
1 parent 9a88261 commit 7a4e526

File tree

1 file changed

+12
-16
lines changed

1 file changed

+12
-16
lines changed

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

Lines changed: 12 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -8,32 +8,30 @@
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 or denominator is not an integer, it is not a proper
11+
// Invalid input: must be integers
1212
if (!Number.isInteger(numerator) || !Number.isInteger(denominator)) {
1313
return false;
1414
}
15-
// if absolute numerator is strictly equal to zero or absolute denominator, it is not a proper fraction
16-
if (Math.abs(numerator) === 0 || Math.abs(denominator) === 0) {
15+
16+
// Denominator cannot be zero
17+
if (denominator === 0) {
1718
return false;
1819
}
19-
// if absolute numerator is strictly equals to absolute, it is not a proper fraction
20-
else if (Math.abs(numerator) === Math.abs(denominator)) {
20+
21+
// Numerator cannot be zero
22+
if (numerator === 0) {
2123
return false;
2224
}
23-
24-
// if absolute Numerator is greater than zero and smaller than absolute denominator, it is a proper fraction
25-
else if (Math.abs(numerator) < Math.abs(denominator)) {
25+
26+
// Proper fraction: absolute numerator smaller than absolute denominator
27+
if (Math.abs(numerator) < Math.abs(denominator)) {
2628
return true;
2729
}
28-
30+
2931
// All other cases are not proper fractions
30-
else {
31-
return false;
32-
}
33-
32+
return false;
3433
}
3534

36-
3735
// The line below allows us to load the isProperFraction function into tests in other files.
3836
// This will be useful in the "rewrite tests with jest" step
3937
module.exports = isProperFraction;
@@ -109,7 +107,6 @@ assertEquals(bothNegative, true);
109107
const zeroDenominator = isProperFraction(1, 0);
110108
assertEquals(zeroDenominator, false);
111109

112-
113110
// Float Numerator check:
114111
// Input: numerator = 2.5, denominator = 3
115112
// Target output: false
@@ -158,4 +155,3 @@ assertEquals(negOneDenominator, false);
158155
// Explanation: 2/4 is a proper fraction because the absolute value of the numerator is less than the absolute value of the denominator.
159156
const twoOverFour = isProperFraction(2, 4);
160157
assertEquals(twoOverFour, true);
161-

0 commit comments

Comments
 (0)