Skip to content

Commit 123c3c2

Browse files
committed
Modified the function isProperFraction() for beter and more accurate results
1 parent d489ca1 commit 123c3c2

File tree

1 file changed

+14
-7
lines changed

1 file changed

+14
-7
lines changed

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

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,14 @@
99

1010
function isProperFraction(numerator, denominator) {
1111
if(typeof(numerator) != "number" || typeof(denominator) != "number") {
12-
return "[ wrong input ]";
12+
throw new Error("[ wrong input ]");
1313
}
14-
if (numerator < denominator ) {
14+
15+
if ( denominator === 0) {
16+
return false;
17+
}
18+
19+
if (Math.abs(numerator) < Math.abs(denominator) ) {
1520
return true;
1621
}
1722
return false;
@@ -46,7 +51,7 @@ const improperFraction = isProperFraction(5, 2);
4651
assertEquals(improperFraction, false);
4752

4853
// Negative Fraction check:
49-
// Input: numerator = -4, denominator = 7
54+
// Input: numerator = -4, denominator = 7 098*&7&&*
5055
// target output: true
5156
// 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.
5257
const negativeFraction = isProperFraction(-4, 7);
@@ -66,12 +71,14 @@ assertEquals(equalFraction, false);
6671

6772
// Try 0 and a negative number check:
6873
// Input: numerator = -10, denominator = 0
69-
// target output: true
74+
// target output: false
7075
const tryZero = isProperFraction(-10, 0);
71-
assertEquals(tryZero, true);
76+
assertEquals(tryZero, false);
7277

7378
// check if inputs are proper numbers
7479
// Input: numerator = "hello", denominator = 4 (any number)
7580
//target output: string "[ wrong input ]"
76-
const tryString = isProperFraction("hello", 4);
77-
assertEquals(tryString, "[ wrong input ]");
81+
82+
// Uncomment to test error handling to see the assertion fail
83+
// const tryString = isProperFraction("hello", 4);
84+
// assertEquals(tryString, "[ wrong input ]");

0 commit comments

Comments
 (0)