Skip to content

Commit 11254cc

Browse files
committed
implement isProperFraction with console.assert tests for all cases
1 parent 82eabc8 commit 11254cc

1 file changed

Lines changed: 26 additions & 0 deletions

File tree

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

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,10 @@
1212

1313
function isProperFraction(numerator, denominator) {
1414
// TODO: Implement this function
15+
// a denominator of 0 is not a proper fraction - it is invalid. divison by 0 undefined.
16+
if (denominator === 0) return false;
17+
// Maths.abs handles negative numbers, -> e.g. -1/2 is a proper fraction, but 1/-2 is not.
18+
return Math.abs(numerator) < Math.abs(denominator);
1519
}
1620

1721
// The line below allows us to load the isProperFraction function into tests in other files.
@@ -31,3 +35,25 @@ function assertEquals(actualOutput, targetOutput) {
3135

3236
// Example: 1/2 is a proper fraction
3337
assertEquals(isProperFraction(1, 2), true);
38+
39+
// TODO: Write tests to cover all cases.
40+
// What combinations of numerators and denominators should you test?
41+
// Using inline style as the result doesn't need to be stored,
42+
// we only need to check if it equals the expected value
43+
44+
// Example: 1/2 is a proper fraction
45+
assertEquals(isProperFraction(1, 2), true);
46+
47+
// Proper fractions: numerator smaller than denominator
48+
assertEquals(isProperFraction(3, 4), true); // normal proper fraction
49+
assertEquals(isProperFraction(-1, 2), true); // negative numerator - still proper
50+
assertEquals(isProperFraction(0, 5), true); // zero numerator - 0/5 = 0, which is proper
51+
52+
// Improper fractions: numerator greater than or equal to denominator
53+
assertEquals(isProperFraction(2, 1), false); // numerator bigger than denominator
54+
assertEquals(isProperFraction(5, 3), false); // normal improper fraction
55+
assertEquals(isProperFraction(4, 4), false); // boundary: equal - 4/4 = 1, a whole number not proper
56+
57+
// Invalid: zero denominator - division by zero is undefined
58+
assertEquals(isProperFraction(1, 0), false);
59+

0 commit comments

Comments
 (0)