|
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 | + return Math.abs(numerator) < denominator; |
14 | 12 | } |
15 | 13 |
|
16 | | -// The line below allows us to load the isProperFraction function into tests in other files. |
17 | | -// This will be useful in the "rewrite tests with jest" step. |
18 | 14 | module.exports = isProperFraction; |
19 | 15 |
|
20 | | -// here's our helper again |
21 | 16 | function assertEquals(actualOutput, targetOutput) { |
22 | 17 | console.assert( |
23 | 18 | actualOutput === targetOutput, |
24 | 19 | `Expected ${actualOutput} to equal ${targetOutput}` |
25 | 20 | ); |
26 | 21 | } |
27 | 22 |
|
28 | | -// Acceptance criteria: |
29 | | - |
30 | | -// Proper Fraction check: |
31 | | -// Input: numerator = 2, denominator = 3 |
32 | | -// target output: true |
33 | | -// Explanation: The fraction 2/3 is a proper fraction, where the numerator is less than the denominator. The function should return true. |
34 | | -const properFraction = isProperFraction(2, 3); |
35 | | -assertEquals(properFraction, true); |
36 | | - |
37 | | -// Improper Fraction check: |
38 | | -// Input: numerator = 5, denominator = 2 |
39 | | -// target output: false |
40 | | -// Explanation: The fraction 5/2 is an improper fraction, where the numerator is greater than or equal to the denominator. The function should return false. |
41 | | -const improperFraction = isProperFraction(5, 2); |
42 | | -assertEquals(improperFraction, false); |
43 | | - |
44 | | -// Negative Fraction check: |
45 | | -// Input: numerator = -4, denominator = 7 |
46 | | -// target output: true |
47 | | -// 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 | | -const negativeFraction = isProperFraction(-4, 7); |
49 | | -// ====> complete with your assertion |
50 | | - |
51 | | -// Equal Numerator and Denominator check: |
52 | | -// Input: numerator = 3, denominator = 3 |
53 | | -// target output: false |
54 | | -// Explanation: The fraction 3/3 is not a proper fraction because the numerator is equal to the denominator. The function should return false. |
55 | | -const equalFraction = isProperFraction(3, 3); |
56 | | -// ====> complete with your assertion |
57 | | - |
58 | | -// Stretch: |
59 | | -// What other scenarios could you test for? |
| 23 | +// Proper Fraction: 2/3 → true |
| 24 | +assertEquals(isProperFraction(2, 3), true); |
| 25 | + |
| 26 | +// Improper Fraction: 5/2 → false |
| 27 | +assertEquals(isProperFraction(5, 2), false); |
| 28 | + |
| 29 | +// Negative Fraction: -4/7 → true (absolute value < denominator) |
| 30 | +assertEquals(isProperFraction(-4, 7), true); |
| 31 | + |
| 32 | +// Equal Numerator and Denominator: 3/3 → false |
| 33 | +assertEquals(isProperFraction(3, 3), false); |
| 34 | + |
| 35 | +console.log("✅ All tests passed!"); |
0 commit comments