|
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) return true; |
| 11 | + // if (numerator < denominator) return true; it will not work for negative numbers |
| 12 | + return Math.abs(numerator) < Math.abs(denominator); |
12 | 13 | } |
13 | 14 |
|
14 | 15 | // here's our helper again |
@@ -41,13 +42,28 @@ assertEquals(improperFraction, false); |
41 | 42 | // 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. |
42 | 43 | const negativeFraction = isProperFraction(-4, 7); |
43 | 44 | // ====> complete with your assertion |
| 45 | +assertEquals(negativeFraction, true); |
44 | 46 |
|
45 | 47 | // Equal Numerator and Denominator check: |
46 | 48 | // Input: numerator = 3, denominator = 3 |
47 | 49 | // target output: false |
48 | 50 | // Explanation: The fraction 3/3 is not a proper fraction because the numerator is equal to the denominator. The function should return false. |
49 | 51 | const equalFraction = isProperFraction(3, 3); |
50 | 52 | // ====> complete with your assertion |
51 | | - |
| 53 | +assertEquals(equalFraction, false); |
52 | 54 | // Stretch: |
53 | 55 | // What other scenarios could you test for? |
| 56 | + |
| 57 | +// Zero Numerator check: |
| 58 | +// Input: numerator = 0, denominator = 5 |
| 59 | +// target output: true |
| 60 | +// Explanation: The fraction 0/5 is a proper fraction because the numerator (0) is less than the denominator (5). The function should return true. |
| 61 | +const zeroNumerator = isProperFraction(0, 5); |
| 62 | +assertEquals(zeroNumerator, true); |
| 63 | + |
| 64 | +// Zero Denominator check: |
| 65 | +// Input: numerator = 4, denominator = 0 |
| 66 | +// target output: false |
| 67 | +// Explanation: A fraction with a zero denominator is undefined. The function should return false to indicate it's not a valid proper fraction. |
| 68 | +const zeroDenominator = isProperFraction(4, 0); |
| 69 | +assertEquals(zeroDenominator, false); |
0 commit comments