|
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 | + if (numerator >= denominator) { |
| 15 | + return false; |
| 16 | + } |
| 17 | + if(numerator == 0){ |
| 18 | + return true; |
| 19 | + } |
| 20 | + if (denominator === 0) { |
| 21 | + return false; |
| 22 | + } |
14 | 23 | } |
15 | 24 |
|
16 | 25 | // The line below allows us to load the isProperFraction function into tests in other files. |
@@ -47,13 +56,48 @@ assertEquals(improperFraction, false); |
47 | 56 | // 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 | 57 | const negativeFraction = isProperFraction(-4, 7); |
49 | 58 | // ====> complete with your assertion |
50 | | - |
| 59 | +assertEquals(negativeFraction, true); |
51 | 60 | // Equal Numerator and Denominator check: |
52 | 61 | // Input: numerator = 3, denominator = 3 |
53 | 62 | // target output: false |
54 | 63 | // Explanation: The fraction 3/3 is not a proper fraction because the numerator is equal to the denominator. The function should return false. |
55 | 64 | const equalFraction = isProperFraction(3, 3); |
56 | 65 | // ====> complete with your assertion |
57 | | - |
| 66 | +assertEquals(equalFraction, false); |
58 | 67 | // Stretch: |
59 | 68 | // What other scenarios could you test for? |
| 69 | +// Zero Numerator check: |
| 70 | +// Input: numerator = 0, denominator = 5 |
| 71 | +// target output: true |
| 72 | +// Explanation: The fraction 0/5 is a proper fraction because the numerator (0) is less than the denominator (5). The function should return true. |
| 73 | +const zeroNumerator = isProperFraction(0, 5); |
| 74 | +// ====> complete with your assertion |
| 75 | +assertEquals(zeroNumerator, true); |
| 76 | +// Zero Denominator check: |
| 77 | +// Input: numerator = 4, denominator = 0 |
| 78 | +// target output: false |
| 79 | +// Explanation: The fraction 4/0 is undefined because division by zero is not allowed. The function should return false. |
| 80 | +const zeroDenominator = isProperFraction(4, 0); |
| 81 | +// ====> complete with your assertion |
| 82 | +assertEquals(zeroDenominator, false); |
| 83 | +// Negative Denominator check: |
| 84 | +// Input: numerator = 3, denominator = -5 |
| 85 | +// target output: true |
| 86 | +// Explanation: The fraction 3/-5 is a proper fraction because the absolute value of the numerator (3) is less than the absolute value of the denominator (5). The function should return true. |
| 87 | +const negativeDenominator = isProperFraction(3, -5); |
| 88 | +// ====> complete with your assertion |
| 89 | +assertEquals(negativeDenominator, true); |
| 90 | +// Both Negative check: |
| 91 | +// Input: numerator = -2, denominator = -6 |
| 92 | +// target output: true |
| 93 | +// Explanation: The fraction -2/-6 is a proper fraction because the absolute value of the numerator (2) is less than the absolute value of the denominator (6). The function should return true. |
| 94 | +const bothNegative = isProperFraction(-2, -6); |
| 95 | +// ====> complete with your assertion |
| 96 | +assertEquals(bothNegative, true); |
| 97 | +// Zero Numerator and Denominator check: |
| 98 | +// Input: numerator = 0, denominator = 0 |
| 99 | +// target output: false |
| 100 | +// Explanation: The fraction 0/0 is undefined. The function should return false. |
| 101 | +const zeroNumeratorDenominator = isProperFraction(0, 0); |
| 102 | +// ====> complete with your assertion |
| 103 | +assertEquals(zeroNumeratorDenominator, false); |
0 commit comments