|
| 1 | +/** |
| 2 | + * |
1 | 3 | // Implement a function isProperFraction, |
2 | 4 | // when given two numbers, a numerator and a denominator, it should return true if |
3 | 5 | // the given numbers form a proper fraction, and false otherwise. |
@@ -31,3 +33,90 @@ function assertEquals(actualOutput, targetOutput) { |
31 | 33 |
|
32 | 34 | // Example: 1/2 is a proper fraction |
33 | 35 | assertEquals(isProperFraction(1, 2), true); |
| 36 | +* |
| 37 | +*/ |
| 38 | + |
| 39 | +/** |
| 40 | + * The code below was written by me, Carlos Abreu |
| 41 | + */ |
| 42 | + |
| 43 | +function isProperFraction(numerator, denominator) { |
| 44 | + // Handle division by zero - not a valid fraction |
| 45 | + if (denominator === 0) { |
| 46 | + return false; |
| 47 | + } |
| 48 | + |
| 49 | + // Check if both numbers are integers |
| 50 | + if (!Number.isInteger(numerator) || !Number.isInteger(denominator)) { |
| 51 | + return false; |
| 52 | + } |
| 53 | + |
| 54 | + // A proper fraction has |numerator| < |denominator| |
| 55 | + return Math.abs(numerator) < Math.abs(denominator); |
| 56 | +} |
| 57 | + |
| 58 | +// The line below allows us to load the isProperFraction function into tests in other files. |
| 59 | +// This will be useful in the "rewrite tests with jest" step. |
| 60 | +module.exports = isProperFraction; |
| 61 | + |
| 62 | +// Here's our helper again |
| 63 | +function assertEquals(actualOutput, targetOutput) { |
| 64 | + console.assert( |
| 65 | + actualOutput === targetOutput, |
| 66 | + `Expected ${actualOutput} to equal ${targetOutput}` |
| 67 | + ); |
| 68 | +} |
| 69 | + |
| 70 | +// TODO: Write tests to cover all cases. |
| 71 | +console.log("Running tests for isProperFraction..."); |
| 72 | + |
| 73 | +// Test 1: Proper fractions with positive numbers |
| 74 | +assertEquals(isProperFraction(1, 2), true); |
| 75 | +assertEquals(isProperFraction(3, 4), true); |
| 76 | +assertEquals(isProperFraction(1, 10), true); |
| 77 | +assertEquals(isProperFraction(0, 5), true); // 0/5 = 0, which is a proper fraction |
| 78 | + |
| 79 | +// Test 2: Improper fractions with positive numbers |
| 80 | +assertEquals(isProperFraction(2, 1), false); |
| 81 | +assertEquals(isProperFraction(5, 3), false); |
| 82 | +assertEquals(isProperFraction(10, 10), false); // Equal numbers are not proper |
| 83 | + |
| 84 | +// Test 3: Proper fractions with negative numbers |
| 85 | +assertEquals(isProperFraction(-1, 2), true); |
| 86 | +assertEquals(isProperFraction(1, -2), true); |
| 87 | +assertEquals(isProperFraction(-3, -4), true); |
| 88 | + |
| 89 | +// Test 4: Improper fractions with negative numbers |
| 90 | +assertEquals(isProperFraction(-3, 2), false); |
| 91 | +assertEquals(isProperFraction(5, -3), false); |
| 92 | +assertEquals(isProperFraction(-5, -3), false); |
| 93 | + |
| 94 | +// Test 5: Fractions equal to 1 or -1 |
| 95 | +assertEquals(isProperFraction(1, 1), false); |
| 96 | +assertEquals(isProperFraction(-1, 1), false); |
| 97 | +assertEquals(isProperFraction(1, -1), false); |
| 98 | + |
| 99 | +// Test 6: Zero numerator |
| 100 | +assertEquals(isProperFraction(0, 1), true); |
| 101 | +assertEquals(isProperFraction(0, -5), true); |
| 102 | +assertEquals(isProperFraction(0, 0), false); // Denominator zero - invalid |
| 103 | + |
| 104 | +// Test 7: Denominator zero |
| 105 | +assertEquals(isProperFraction(5, 0), false); |
| 106 | +assertEquals(isProperFraction(-3, 0), false); |
| 107 | + |
| 108 | +// Test 8: Non-integer inputs |
| 109 | +assertEquals(isProperFraction(1.5, 2), false); |
| 110 | +assertEquals(isProperFraction(1, 2.5), false); |
| 111 | +assertEquals(isProperFraction(1.5, 2.5), false); |
| 112 | + |
| 113 | +// Test 9: Large numbers |
| 114 | +assertEquals(isProperFraction(1000000, 2000000), true); |
| 115 | +assertEquals(isProperFraction(2000000, 1000000), false); |
| 116 | + |
| 117 | +// Test 10: Edge cases with very small/large differences |
| 118 | +assertEquals(isProperFraction(999999, 1000000), true); |
| 119 | +assertEquals(isProperFraction(1000000, 999999), false); |
| 120 | + |
| 121 | +console.log("All tests completed!"); |
| 122 | + |
0 commit comments