Skip to content

Commit 7fd6b77

Browse files
committed
Add validation for denominator and expand test cases in isProperFraction function
1 parent 1c09d41 commit 7fd6b77

File tree

1 file changed

+26
-1
lines changed

1 file changed

+26
-1
lines changed

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

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,12 @@
88
// write one test at a time, and make it pass, build your solution up methodically
99

1010
function isProperFraction(numerator, denominator) {
11-
if (numerator < denominator) {
11+
if (denominator === 0) {
12+
return "Invalid — denominator cannot be zero";
13+
} else if (numerator * numerator < denominator * denominator) {
1214
return true;
15+
} else {
16+
return false;
1317
}
1418
}
1519

@@ -46,14 +50,35 @@ assertEquals(improperFraction, false);
4650
// target output: true
4751
// 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.
4852
const negativeFraction = isProperFraction(-4, 7);
53+
assertEquals(negativeFraction, true);
4954
// ====> complete with your assertion
5055

5156
// Equal Numerator and Denominator check:
5257
// Input: numerator = 3, denominator = 3
5358
// target output: false
5459
// Explanation: The fraction 3/3 is not a proper fraction because the numerator is equal to the denominator. The function should return false.
5560
const equalFraction = isProperFraction(3, 3);
61+
assertEquals(equalFraction, false);
5662
// ====> complete with your assertion
5763

5864
// Stretch:
5965
// What other scenarios could you test for?
66+
//Stretch 1 - Zero numerator
67+
const zeroNumerator = isProperFraction(0, 5);
68+
assertEquals(zeroNumerator, true);
69+
70+
//Stretch 2 - Negative denominator
71+
const negativeDenominator = isProperFraction(4, -7);
72+
assertEquals(negativeDenominator, true);
73+
74+
//Stretch 3 - Both numerator and denominator negative
75+
const bothNegative = isProperFraction(-2, -3);
76+
assertEquals(bothNegative, true);
77+
78+
//Stretch 4 - Zero denominator (edge case)
79+
const zeroDenominator = isProperFraction(3, 0);
80+
assertEquals(zeroDenominator, "Invalid — denominator cannot be zero");
81+
82+
//Stretch 5 - Both numerator and denominator negative (numerator absolute value > denominator absolute value)
83+
const bothNegative2 = isProperFraction(-5, -3);
84+
assertEquals(bothNegative2, false);

0 commit comments

Comments
 (0)