|
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) { |
12 | | - return true; |
13 | | - } |
| 11 | + // A proper fraction is defined as a fraction a/b where: |
| 12 | + // 1. The numerator (a) is a positive integer |
| 13 | + // 2. The denominator (b) is a positive integer |
| 14 | + // 3. The absolute value of the numerator is less than the absolute value of the denominator: |
| 15 | + // |a| < |b| |
| 16 | + return Math.abs(numerator) < Math.abs(denominator); |
14 | 17 | } |
15 | 18 |
|
16 | 19 | // The line below allows us to load the isProperFraction function into tests in other files. |
@@ -46,14 +49,22 @@ assertEquals(improperFraction, false); |
46 | 49 | // target output: true |
47 | 50 | // 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 | 51 | const negativeFraction = isProperFraction(-4, 7); |
49 | | -// ====> 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 | | -// ====> complete with your assertion |
| 59 | +assertEquals(equalFraction, false); |
57 | 60 |
|
58 | 61 | // Stretch: |
59 | 62 | // What other scenarios could you test for? |
| 63 | + |
| 64 | +// Given the definition of a proper fraction, cases involving values such as zero or floating-point numbers should be considered false as they are not positive integers (natural numbers): |
| 65 | +// - Where either the numerator or denominator is zero, the function should return false. |
| 66 | +// - Floating-point numbers in the function should return false, as proper fractions are defined for integers only. |
| 67 | + |
| 68 | +// The method `Number.isInteger()` can be used to check for positive integers by: |
| 69 | +// - Using `Math.abs()` to handle negative values when comparing numerator and denominator. |
| 70 | +// - Then checking if both numerator and denominator are positive integers using `Number.isInteger()` and greater than zero. |
0 commit comments