|
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.sign(numerator) === -1 || Math.sign(denominator) === -1) { |
| 12 | + if (Math.abs(numerator) < Math.abs(denominator)) { |
| 13 | + return true; |
| 14 | + } |
| 15 | + } else if (numerator === 0) { |
| 16 | + return false; |
| 17 | + } |
| 18 | + |
11 | 19 | if (numerator < denominator) { |
12 | 20 | return true; |
| 21 | + } else if (numerator > denominator) { |
| 22 | + return false; |
| 23 | + } else if (numerator === denominator) { |
| 24 | + return false; |
13 | 25 | } |
14 | 26 | } |
15 | 27 |
|
16 | 28 | // The line below allows us to load the isProperFraction function into tests in other files. |
17 | 29 | // This will be useful in the "rewrite tests with jest" step. |
18 | | -module.exports = isProperFraction; |
| 30 | +//module.exports = isProperFraction; |
19 | 31 |
|
20 | 32 | // here's our helper again |
21 | 33 | function assertEquals(actualOutput, targetOutput) { |
@@ -45,15 +57,20 @@ assertEquals(improperFraction, false); |
45 | 57 | // Input: numerator = -4, denominator = 7 |
46 | 58 | // target output: true |
47 | 59 | // 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. |
48 | | -const negativeFraction = isProperFraction(-4, 7); |
| 60 | +const negativeFraction = isProperFraction(-4, -7); |
49 | 61 | // ====> complete with your assertion |
| 62 | +assertEquals(negativeFraction, true); |
50 | 63 |
|
51 | 64 | // Equal Numerator and Denominator check: |
52 | 65 | // Input: numerator = 3, denominator = 3 |
53 | 66 | // target output: false |
54 | 67 | // Explanation: The fraction 3/3 is not a proper fraction because the numerator is equal to the denominator. The function should return false. |
55 | 68 | const equalFraction = isProperFraction(3, 3); |
56 | 69 | // ====> complete with your assertion |
| 70 | +assertEquals(equalFraction, false); |
57 | 71 |
|
58 | 72 | // Stretch: |
59 | 73 | // What other scenarios could you test for? |
| 74 | +//we can test if the numerator is 0 |
| 75 | +const zeroFraction = isProperFraction(0, 4); |
| 76 | +assertEquals(zeroFraction, false); |
0 commit comments