|
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 | + } else { |
| 14 | + return false; |
13 | 15 | } |
14 | 16 | } |
15 | 17 |
|
@@ -47,13 +49,50 @@ assertEquals(improperFraction, false); |
47 | 49 | // 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 | 50 | const negativeFraction = isProperFraction(-4, 7); |
49 | 51 | // ====> complete with your assertion |
| 52 | +assertEquals(negativeFraction, true); |
50 | 53 |
|
51 | 54 | // Equal Numerator and Denominator check: |
52 | 55 | // Input: numerator = 3, denominator = 3 |
53 | 56 | // target output: false |
54 | 57 | // Explanation: The fraction 3/3 is not a proper fraction because the numerator is equal to the denominator. The function should return false. |
55 | 58 | const equalFraction = isProperFraction(3, 3); |
56 | 59 | // ====> complete with your assertion |
| 60 | +assertEquals(equalFraction, false); |
57 | 61 |
|
58 | 62 | // Stretch: |
59 | 63 | // What other scenarios could you test for? |
| 64 | + |
| 65 | +// Stretch 1: negative denominator |
| 66 | +// Input: numerator = 2, denominator = -3 |
| 67 | +// target output: true |
| 68 | +// Explanation: The fraction 2/-3 is a proper fraction because the absolute value of denominator (3) is larger than the numerator (2). |
| 69 | +const negativeDenominator = isProperFraction(2, -3); |
| 70 | +assertEquals(negativeDenominator, true); |
| 71 | + |
| 72 | +// Stretch 2: zero numerator |
| 73 | +// Input: numerator = 0, denominator = 5 |
| 74 | +// target output: true |
| 75 | +// Explanation: The fraction 0/5 is a proper fraction because the absolute value of numerator (0) is less than the denominator (5). |
| 76 | +const zeroNumerator = isProperFraction(0, 5); |
| 77 | +assertEquals(zeroNumerator, true); |
| 78 | +// Stretch 3: zero denominator - this is mathematically undefined but we can decide how we want to handle it |
| 79 | +const zeroDenominator = isProperFraction(5, 0); |
| 80 | +assertEquals(zeroDenominator, false); |
| 81 | +// Stretch 4: both zero |
| 82 | +const bothZero = isProperFraction(0, 0); |
| 83 | +assertEquals(bothZero, false); |
| 84 | +// Stretch 5: negative numerator and denominator |
| 85 | +const negativeNumeratorAndDenominator = isProperFraction(-3, -5); |
| 86 | +assertEquals(negativeNumeratorAndDenominator, true); |
| 87 | +// Stretch 6: improper negative numerator and denominator |
| 88 | +const properNegativeNumeratorAndDenominator = isProperFraction(-3, -2); |
| 89 | +assertEquals(properNegativeNumeratorAndDenominator, false); |
| 90 | +// Stretch 7: decimal values |
| 91 | +const decimalValues = isProperFraction(2.5, 3.5); |
| 92 | +assertEquals(decimalValues, true); |
| 93 | +// Stretch 8: improper decimal values |
| 94 | +const improperDecimalValues = isProperFraction(3.5, 2.5); |
| 95 | +assertEquals(improperDecimalValues, false); |
| 96 | +// Stretch 9: invalid inputs (non-numeric values) |
| 97 | +const invalidInputs = isProperFraction("a", 2); |
| 98 | +assertEquals(invalidInputs, false); |
0 commit comments