|
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 (Math.abs(numerator) < Math.abs(denominator)) { |
| 11 | + const absNumerator = Math.abs(numerator); |
| 12 | + const absDenominator = Math.abs(denominator); |
| 13 | + |
| 14 | + if (absDenominator === 0) { |
| 15 | + return `you cannot have zero as a denominator`; |
| 16 | + } |
| 17 | + |
| 18 | + if (absNumerator < absDenominator) { |
12 | 19 | return true; |
13 | | - } else if (numerator > denominator) { |
14 | | - return false; |
15 | | - } else if (numerator === denominator) { |
16 | | - return false; |
17 | | - } else if (numerator === 0) { |
18 | | - return false; |
19 | 20 | } |
| 21 | + |
| 22 | + return false; |
20 | 23 | } |
21 | 24 |
|
22 | 25 | // The line below allows us to load the isProperFraction function into tests in other files. |
@@ -67,4 +70,8 @@ assertEquals(equalFraction, false); |
67 | 70 | // What other scenarios could you test for? |
68 | 71 | //we can test if the numerator is 0 |
69 | 72 | const zeroFraction = isProperFraction(0, 4); |
70 | | -assertEquals(zeroFraction, false); |
| 73 | +assertEquals(zeroFraction, true); |
| 74 | + |
| 75 | +//we can test if the denominator is 0 |
| 76 | +const indefiniteFraction = isProperFraction(3, 0); |
| 77 | +assertEquals(indefiniteFraction, `you cannot have zero as a denominator`); |
0 commit comments