Skip to content

Commit 2c53190

Browse files
committed
Implement isProperFraction and add assertion tests
1 parent c66e7d4 commit 2c53190

File tree

1 file changed

+27
-1
lines changed

1 file changed

+27
-1
lines changed

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

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,17 @@
1111
// execute the code to ensure all tests pass.
1212

1313
function isProperFraction(numerator, denominator) {
14-
// TODO: Implement this function
14+
// A fraction with denominator 0 is invalid
15+
if (denominator === 0) {
16+
return false;
17+
}
18+
19+
// A proper fraction has absolute numerator smaller than absolute denominator
20+
if (Math.abs(numerator) < Math.abs(denominator)) {
21+
return true;
22+
}
23+
24+
return false;
1525
}
1626

1727
// The line below allows us to load the isProperFraction function into tests in other files.
@@ -31,3 +41,19 @@ function assertEquals(actualOutput, targetOutput) {
3141

3242
// Example: 1/2 is a proper fraction
3343
assertEquals(isProperFraction(1, 2), true);
44+
45+
// Proper fractions
46+
assertEquals(isProperFraction(3, 5), true);
47+
assertEquals(isProperFraction(-2, 7), true);
48+
assertEquals(isProperFraction(2, -7), true);
49+
50+
// Improper fractions
51+
assertEquals(isProperFraction(5, 5), false);
52+
assertEquals(isProperFraction(7, 3), false);
53+
assertEquals(isProperFraction(-8, 4), false);
54+
55+
// Zero numerator
56+
assertEquals(isProperFraction(0, 5), true);
57+
58+
// Invalid fraction (denominator 0)
59+
assertEquals(isProperFraction(2, 0), false);

0 commit comments

Comments
 (0)