Skip to content

Commit f52bd8d

Browse files
authored
Fix the code
Refactor isProperFraction function to include input type checks and handle division by zero. Update logic to determine if a fraction is proper based on the value of the numerator and denominator.
1 parent 991832e commit f52bd8d

File tree

1 file changed

+10
-13
lines changed

1 file changed

+10
-13
lines changed

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

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -8,23 +8,20 @@
88
// write one test at a time, and make it pass, build your solution up methodically
99

1010
function isProperFraction(numerator, denominator) {
11+
if (typeof numerator !== "number" || typeof denominator !== "number") {
12+
return false; // Invalid input types
13+
}
14+
1115
if (denominator === 0) {
12-
return false;
13-
}
14-
return Math.abs(numerator) < Math.abs(denominator);
16+
return false; // Division by zero is undefined
17+
}
18+
19+
const value = numerator / denominator;
20+
return Math.abs(value) < 1; // Proper fraction if the value is between -1 and 1
1521
}
1622

17-
// The line below allows us to load the isProperFraction function into tests in other files.
18-
// This will be useful in the "rewrite tests with jest" step.
1923
module.exports = isProperFraction;
2024

21-
// here's our helper again
22-
function assertEquals(actualOutput, targetOutput) {
23-
console.assert(
24-
actualOutput === targetOutput,
25-
`Expected ${actualOutput} to equal ${targetOutput}`
26-
);
27-
}
2825

2926
// Acceptance criteria:
3027

@@ -99,4 +96,4 @@ denominator= -2;
9996
console.log(numerator + "/" + denominator + " check =", isProperFraction(numerator, denominator));
10097
numerator= -3;
10198
denominator= -7;
102-
console.log(numerator + "/" + denominator + " check =", isProperFraction(numerator, denominator));
99+
console.log(numerator + "/" + denominator + " check =", isProperFraction(numerator, denominator));

0 commit comments

Comments
 (0)