-
-
Notifications
You must be signed in to change notification settings - Fork 337
Expand file tree
/
Copy path2-is-proper-fraction.js
More file actions
70 lines (54 loc) · 2.16 KB
/
2-is-proper-fraction.js
File metadata and controls
70 lines (54 loc) · 2.16 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
// Implement a function isProperFraction.
// When given two numbers, a numerator and a denominator, it should return true
// if the given numbers form a proper fraction, and false otherwise.
// Assumption: The parameters are valid numbers (not NaN or Infinity).
// Definition:
// A proper fraction is a fraction where:
// - the denominator is not zero
// - both numbers are non-negative
// - the numerator is smaller than the denominator
// Acceptance criteria:
// After implementing the function, write tests to cover all cases
// and run the code to ensure all tests pass.
function isProperFraction(numerator, denominator) {
// A fraction with denominator 0 is invalid
if (denominator === 0) {
return false;
}
// Negative values are not allowed
if (numerator < 0 || denominator < 0) {
return false;
}
// A proper fraction must have numerator smaller than denominator
if (numerator < denominator) {
return true;
}
// All other cases are not proper fractions
return false;
}
// The line below allows us to load the isProperFraction function into tests in other files.
// This will be useful in the "rewrite tests with jest" step.
module.exports = isProperFraction;
// Helper function for simple assertions in this file
function assertEquals(actualOutput, targetOutput) {
console.assert(
actualOutput === targetOutput,
`Expected ${actualOutput} to equal ${targetOutput}`
);
}
// Tests to cover different combinations of numerators and denominators
// Example: 1/2 is a proper fraction
assertEquals(isProperFraction(1, 2), true);
// Proper fractions (numerator smaller than denominator)
assertEquals(isProperFraction(3, 5), true);
assertEquals(isProperFraction(2, 7), true);
// Improper fractions (numerator greater than or equal to denominator)
assertEquals(isProperFraction(5, 5), false);
assertEquals(isProperFraction(7, 3), false);
// Negative numbers should return false
assertEquals(isProperFraction(-2, 7), false);
assertEquals(isProperFraction(2, -7), false);
// Zero numerator is allowed if denominator is positive
assertEquals(isProperFraction(0, 5), true);
// Invalid fraction (denominator is zero)
assertEquals(isProperFraction(2, 0), false);