|
5 | 5 | // Written here like this: 1/2 == Numerator/Denominator |
6 | 6 | // the first test and first case is written for you |
7 | 7 | // complete the rest of the tests and cases |
8 | | -// write one test at a time, and make it pass, build your solution up methodically |
| 8 | +// write one test at a time, and make it pass, build your solution up |
9 | 9 |
|
10 | 10 | function isProperFraction(numerator, denominator) { |
| 11 | + if (denominator === 0) return false; |
| 12 | + if (numerator < 1) numerator = numerator * -1; |
| 13 | + if (denominator < 1) denominator = Math.abs(denominator); |
| 14 | + |
11 | 15 | if (numerator < denominator) { |
12 | 16 | return true; |
| 17 | + } else { |
| 18 | + return false; |
13 | 19 | } |
14 | 20 | } |
15 | 21 |
|
@@ -47,13 +53,20 @@ assertEquals(improperFraction, false); |
47 | 53 | // 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 | 54 | const negativeFraction = isProperFraction(-4, 7); |
49 | 55 | // ====> complete with your assertion |
| 56 | +assertEquals(negativeFraction, true); |
50 | 57 |
|
51 | 58 | // Equal Numerator and Denominator check: |
52 | 59 | // Input: numerator = 3, denominator = 3 |
53 | 60 | // target output: false |
54 | 61 | // Explanation: The fraction 3/3 is not a proper fraction because the numerator is equal to the denominator. The function should return false. |
55 | 62 | const equalFraction = isProperFraction(3, 3); |
56 | 63 | // ====> complete with your assertion |
57 | | - |
| 64 | +assertEquals(equalFraction, false); |
58 | 65 | // Stretch: |
59 | 66 | // What other scenarios could you test for? |
| 67 | +//Negative fraction check |
| 68 | +//Input: numerator=-4, denominator=2; |
| 69 | +//target output: false |
| 70 | +// Explanation: The fraction -4/2 is not a proper fraction because the absolute value of the numerator (4) is more than the denominator (2). The function should return false. |
| 71 | +const negativeFractionCase2 = isProperFraction(-4, 2); |
| 72 | +assertEquals(negativeFractionCase2, false); |
0 commit comments