|
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 < denominator) { |
| 11 | + if (Math.abs(numerator) < Math.abs(denominator)) { |
12 | 12 | return true; |
13 | 13 | } |
| 14 | + return false; |
14 | 15 | } |
15 | 16 |
|
16 | 17 | // The line below allows us to load the isProperFraction function into tests in other files. |
@@ -47,13 +48,34 @@ assertEquals(improperFraction, false); |
47 | 48 | // 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 | 49 | const negativeFraction = isProperFraction(-4, 7); |
49 | 50 | // ====> complete with your assertion |
| 51 | +assertEquals(negativeFraction, true); |
50 | 52 |
|
51 | 53 | // Equal Numerator and Denominator check: |
52 | 54 | // Input: numerator = 3, denominator = 3 |
53 | 55 | // target output: false |
54 | 56 | // Explanation: The fraction 3/3 is not a proper fraction because the numerator is equal to the denominator. The function should return false. |
55 | 57 | const equalFraction = isProperFraction(3, 3); |
56 | 58 | // ====> complete with your assertion |
| 59 | +assertEquals(equalFraction, false); |
57 | 60 |
|
58 | 61 | // Stretch: |
59 | | -// What other scenarios could you test for? |
| 62 | +// What other scenarios could you test for? Zero numerator => always proper |
| 63 | +const zeroNumerator = isProperFraction(0, 10); |
| 64 | +assertEquals(zeroNumerator, true); |
| 65 | + |
| 66 | +//Negarive denominator => use absoluet values |
| 67 | +const negativeDenominator = isProperFraction(3, -5); |
| 68 | +assertEquals(negativeDenominator, true); |
| 69 | + |
| 70 | +//Both negatives |
| 71 | +const bothNegative = isProperFraction(-2, -3); |
| 72 | +assertEquals(bothNegative, true); |
| 73 | + |
| 74 | +//fractions like 8/8 should be false |
| 75 | +const improperEqual = isProperFraction(9, 9); |
| 76 | +assertEquals(improperEqual, false); |
| 77 | + |
| 78 | +//zero denominator - maths says undefined so can return false or throw error |
| 79 | +const zeroDenominator = isProperFraction(1, 0); |
| 80 | +assertEquals(zeroDenominator, false); |
| 81 | + |
0 commit comments