|
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 | + if (typeof numerator !== "number" || typeof denominator !== "number") |
| 12 | + return "Use only numbers"; |
| 13 | + |
| 14 | + return Math.abs(numerator) < Math.abs(denominator); |
14 | 15 | } |
15 | 16 |
|
16 | 17 | // The line below allows us to load the isProperFraction function into tests in other files. |
@@ -46,14 +47,43 @@ assertEquals(improperFraction, false); |
46 | 47 | // target output: true |
47 | 48 | // 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 | 49 | const negativeFraction = isProperFraction(-4, 7); |
49 | | -// ====> complete with your assertion |
| 50 | +assertEquals(negativeFraction, true); |
50 | 51 |
|
51 | 52 | // Equal Numerator and Denominator check: |
52 | 53 | // Input: numerator = 3, denominator = 3 |
53 | 54 | // target output: false |
54 | 55 | // Explanation: The fraction 3/3 is not a proper fraction because the numerator is equal to the denominator. The function should return false. |
55 | 56 | const equalFraction = isProperFraction(3, 3); |
| 57 | +assertEquals(equalFraction, false); |
56 | 58 | // ====> complete with your assertion |
57 | 59 |
|
58 | 60 | // Stretch: |
59 | 61 | // What other scenarios could you test for? |
| 62 | + |
| 63 | +// Negative Fraction check: |
| 64 | +// Input: numerator = 4, denominator = -7 |
| 65 | +// target output: true |
| 66 | +// Explanation: The fraction 4/-7 is a proper fraction because the numerator (4) is less than the absolute value of the denominator (7). The function should return true. |
| 67 | +const negativeFraction2 = isProperFraction(4, -7); |
| 68 | +assertEquals(negativeFraction2, true); |
| 69 | + |
| 70 | +// Negative Fraction check: |
| 71 | +// Input: numerator = -4, denominator = -7 |
| 72 | +// target output: true |
| 73 | +// Explanation: The fraction -4/-7 is a proper fraction because the absolute value of the numerator (4) is less than the absolute value of the denominator (7). The function should return true. |
| 74 | +const negativeFraction3 = isProperFraction(-4, -7); |
| 75 | +assertEquals(negativeFraction3, true); |
| 76 | + |
| 77 | +//Invalid input |
| 78 | +//Input:numerator = "q", denominator = 7 |
| 79 | +// target output: message |
| 80 | +//Explanation: the fraction only takes numbers |
| 81 | +const notANumber = isProperFraction("q", 7); |
| 82 | +assertEquals(notANumber, "Use only numbers"); |
| 83 | + |
| 84 | +//Invalid input |
| 85 | +//Input:numerator = 7, denominator = "q" |
| 86 | +// target output: message |
| 87 | +//Explanation: the fraction only takes numbers |
| 88 | +const notANumber2 = isProperFraction(7, "q"); |
| 89 | +assertEquals(notANumber2, "Use only numbers"); |
0 commit comments