|
8 | 8 | // write one test at a time, and make it pass, build your solution up methodically |
9 | 9 |
|
10 | 10 | function isProperFraction(numerator, denominator) { |
11 | | - //if numerator or denominator is not an integer, it is not a proper |
| 11 | + // Invalid input: must be integers |
12 | 12 | if (!Number.isInteger(numerator) || !Number.isInteger(denominator)) { |
13 | 13 | return false; |
14 | 14 | } |
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) { |
17 | 18 | return false; |
18 | 19 | } |
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) { |
21 | 23 | return false; |
22 | 24 | } |
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)) { |
26 | 28 | return true; |
27 | 29 | } |
28 | | - |
| 30 | + |
29 | 31 | // All other cases are not proper fractions |
30 | | - else { |
31 | | - return false; |
32 | | - } |
33 | | - |
| 32 | + return false; |
34 | 33 | } |
35 | 34 |
|
36 | | - |
37 | 35 | // The line below allows us to load the isProperFraction function into tests in other files. |
38 | 36 | // This will be useful in the "rewrite tests with jest" step |
39 | 37 | module.exports = isProperFraction; |
@@ -109,7 +107,6 @@ assertEquals(bothNegative, true); |
109 | 107 | const zeroDenominator = isProperFraction(1, 0); |
110 | 108 | assertEquals(zeroDenominator, false); |
111 | 109 |
|
112 | | - |
113 | 110 | // Float Numerator check: |
114 | 111 | // Input: numerator = 2.5, denominator = 3 |
115 | 112 | // Target output: false |
@@ -158,4 +155,3 @@ assertEquals(negOneDenominator, false); |
158 | 155 | // Explanation: 2/4 is a proper fraction because the absolute value of the numerator is less than the absolute value of the denominator. |
159 | 156 | const twoOverFour = isProperFraction(2, 4); |
160 | 157 | assertEquals(twoOverFour, true); |
161 | | - |
0 commit comments