Skip to content

Commit b0c441a

Browse files
committed
2-is-proper-fraction.js committed
1 parent 753f339 commit b0c441a

File tree

1 file changed

+89
-0
lines changed

1 file changed

+89
-0
lines changed

Sprint-3/1-implement-and-rewrite-tests/implement/2-is-proper-fraction.js

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
/**
2+
*
13
// Implement a function isProperFraction,
24
// when given two numbers, a numerator and a denominator, it should return true if
35
// the given numbers form a proper fraction, and false otherwise.
@@ -31,3 +33,90 @@ function assertEquals(actualOutput, targetOutput) {
3133
3234
// Example: 1/2 is a proper fraction
3335
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

Comments
 (0)