Skip to content

Commit 6563d24

Browse files
committed
2-is-proper-fraction.js committed
1 parent 5af0fcb commit 6563d24

File tree

1 file changed

+76
-0
lines changed

1 file changed

+76
-0
lines changed

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

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
/**
2+
* Original file:
3+
*
14
// Implement a function isProperFraction,
25
// when given two numbers, a numerator and a denominator, it should return true if
36
// the given numbers form a proper fraction, and false otherwise.
@@ -31,3 +34,76 @@ function assertEquals(actualOutput, targetOutput) {
3134
3235
// Example: 1/2 is a proper fraction
3336
assertEquals(isProperFraction(1, 2), true);
37+
*
38+
*/
39+
40+
// Implementation of a function isProperFraction,
41+
// when given two numbers, a numerator and a denominator, it should return true if
42+
// the given numbers form a proper fraction, and false otherwise.
43+
44+
// Assumption: The parameters are valid numbers (not NaN or Infinity).
45+
46+
function isProperFraction(numerator, denominator) {
47+
// Check if denominator is zero - not a valid fraction
48+
if (denominator === 0) {
49+
return false;
50+
}
51+
52+
// Check if both numbers are integers
53+
if (!Number.isInteger(numerator) || !Number.isInteger(denominator)) {
54+
return false;
55+
}
56+
57+
// Check if absolute value of numerator is less than absolute value of denominator
58+
return Math.abs(numerator) < Math.abs(denominator);
59+
}
60+
61+
module.exports = isProperFraction;
62+
63+
// Here's our helper again
64+
function assertEquals(actualOutput, targetOutput) {
65+
console.assert(
66+
actualOutput === targetOutput,
67+
`Expected ${actualOutput} to equal ${targetOutput}`
68+
);
69+
}
70+
71+
// TODO: Write tests to cover all cases.
72+
console.log("Running tests for isProperFraction...");
73+
74+
// Test 1: Proper fractions with positive numbers
75+
assertEquals(isProperFraction(1, 2), true); // 1/2 is proper
76+
assertEquals(isProperFraction(3, 4), true); // 3/4 is proper
77+
assertEquals(isProperFraction(5, 8), true); // 5/8 is proper
78+
assertEquals(isProperFraction(0, 5), true); // 0/5 is proper (0 < 5)
79+
80+
// Test 2: Improper fractions with positive numbers
81+
assertEquals(isProperFraction(3, 2), false); // 3/2 is improper
82+
assertEquals(isProperFraction(4, 3), false); // 4/3 is improper
83+
assertEquals(isProperFraction(5, 5), false); // 5/5 is equal, so improper
84+
85+
// Test 3: Fractions with negative numbers
86+
assertEquals(isProperFraction(-1, 2), true); // |-1| < 2, so proper
87+
assertEquals(isProperFraction(1, -2), true); // 1 < |-2|, so proper
88+
assertEquals(isProperFraction(-3, -4), true); // |-3| < |-4|, so proper
89+
assertEquals(isProperFraction(-3, 2), false); // |-3| > 2, so improper
90+
assertEquals(isProperFraction(3, -2), false); // 3 > |-2|, so improper
91+
assertEquals(isProperFraction(-4, -3), false); // |-4| > |-3|, so improper
92+
93+
// Test 4: Edge cases with zero
94+
assertEquals(isProperFraction(0, 1), true); // 0/1 is proper
95+
assertEquals(isProperFraction(0, -5), true); // 0/ -5 is proper
96+
assertEquals(isProperFraction(5, 0), false); // Denominator cannot be zero
97+
assertEquals(isProperFraction(0, 0), false); // Denominator cannot be zero
98+
99+
// Test 5: Non-integer inputs
100+
assertEquals(isProperFraction(1.5, 2), false); // Numerator not integer
101+
assertEquals(isProperFraction(1, 2.5), false); // Denominator not integer
102+
assertEquals(isProperFraction(1.5, 2.5), false); // Both not integers
103+
104+
// Test 6: Large numbers
105+
assertEquals(isProperFraction(100, 101), true); // 100 < 101
106+
assertEquals(isProperFraction(1000, 999), false); // 1000 > 999
107+
assertEquals(isProperFraction(-100, 101), true); // |-100| < 101
108+
109+
console.log("Tests completed!");

0 commit comments

Comments
 (0)