|
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 (denominator === 0) { |
| 12 | + return "Invalid — denominator cannot be zero"; |
| 13 | + } else if (numerator * numerator < denominator * denominator) { |
12 | 14 | return true; |
| 15 | + } else { |
| 16 | + return false; |
13 | 17 | } |
14 | 18 | } |
15 | 19 |
|
@@ -46,14 +50,35 @@ assertEquals(improperFraction, false); |
46 | 50 | // target output: true |
47 | 51 | // 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 | 52 | const negativeFraction = isProperFraction(-4, 7); |
| 53 | +assertEquals(negativeFraction, true); |
49 | 54 | // ====> complete with your assertion |
50 | 55 |
|
51 | 56 | // Equal Numerator and Denominator check: |
52 | 57 | // Input: numerator = 3, denominator = 3 |
53 | 58 | // target output: false |
54 | 59 | // Explanation: The fraction 3/3 is not a proper fraction because the numerator is equal to the denominator. The function should return false. |
55 | 60 | const equalFraction = isProperFraction(3, 3); |
| 61 | +assertEquals(equalFraction, false); |
56 | 62 | // ====> complete with your assertion |
57 | 63 |
|
58 | 64 | // Stretch: |
59 | 65 | // What other scenarios could you test for? |
| 66 | +//Stretch 1 - Zero numerator |
| 67 | +const zeroNumerator = isProperFraction(0, 5); |
| 68 | +assertEquals(zeroNumerator, true); |
| 69 | + |
| 70 | +//Stretch 2 - Negative denominator |
| 71 | +const negativeDenominator = isProperFraction(4, -7); |
| 72 | +assertEquals(negativeDenominator, true); |
| 73 | + |
| 74 | +//Stretch 3 - Both numerator and denominator negative |
| 75 | +const bothNegative = isProperFraction(-2, -3); |
| 76 | +assertEquals(bothNegative, true); |
| 77 | + |
| 78 | +//Stretch 4 - Zero denominator (edge case) |
| 79 | +const zeroDenominator = isProperFraction(3, 0); |
| 80 | +assertEquals(zeroDenominator, "Invalid — denominator cannot be zero"); |
| 81 | + |
| 82 | +//Stretch 5 - Both numerator and denominator negative (numerator absolute value > denominator absolute value) |
| 83 | +const bothNegative2 = isProperFraction(-5, -3); |
| 84 | +assertEquals(bothNegative2, false); |
0 commit comments