-
-
Notifications
You must be signed in to change notification settings - Fork 337
Expand file tree
/
Copy path2-is-proper-fraction.js
More file actions
55 lines (40 loc) · 1.7 KB
/
2-is-proper-fraction.js
File metadata and controls
55 lines (40 loc) · 1.7 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
// 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).
// Acceptance criteria:
// After you have implemented the function, write tests to cover all the cases, and
// execute the code to ensure all tests pass.
function isProperFraction(numerator, denominator) {
if (denominator === 0) {
return false;
}
return Math.abs(numerator) < Math.abs(denominator);
}
// The line below allows us to load the isProperFraction function into tests in other files.
module.exports = isProperFraction;
// Helper function
function assertEquals(actualOutput, targetOutput) {
console.assert(
actualOutput === targetOutput,
`Expected ${actualOutput} to equal ${targetOutput}`
);
}
console.log("Starting tests for isProperFraction...");
// 1. Proper fraction: 1/2
assertEquals(isProperFraction(1, 2), true);
// 2. Improper fraction: numerator greater than denominator
assertEquals(isProperFraction(3, 2), false);
// 3. Improper fraction: numerator equals denominator
assertEquals(isProperFraction(5, 5), false);
// 4. Proper fraction with negative numerator
assertEquals(isProperFraction(-1, 4), true);
// 5. Proper fraction with negative denominator
assertEquals(isProperFraction(2, -5), true);
// 6. Improper fraction with negative numerator
assertEquals(isProperFraction(-7, 3), false);
// 7. Zero numerator — 0/5 is proper because 0 < 5
assertEquals(isProperFraction(0, 5), true);
// 8. Zero denominator — invalid, return false
assertEquals(isProperFraction(1, 0), false);
console.log("All tests completed!");