-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path2-is-proper-fraction.js
More file actions
152 lines (128 loc) · 5.2 KB
/
2-is-proper-fraction.js
File metadata and controls
152 lines (128 loc) · 5.2 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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
// Implement a function isProperFraction
// Write assertions for your function to check it works in different cases
// Terms:
// Fractions: https://www.bbc.co.uk/bitesize/topics/zt9n6g8/articles/zjxpp4j
// Written here like this: 1/2 == Numerator/Denominator
// the first test and first case is written for you
// complete the rest of the tests and cases
// write one test at a time, and make it pass, build your solution up methodically
function isProperFraction(numerator, denominator) {
// Invalid input: must be integers
if (!Number.isInteger(numerator) || !Number.isInteger(denominator)) {
return false;
}
// Denominator cannot be zero
if (denominator === 0) {
return false;
}
// Numerator cannot be zero
if (numerator === 0) {
return false;
}
// Proper fraction: absolute numerator smaller than absolute denominator
if (Math.abs(numerator) < Math.abs(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;
// here's our helper again
function assertEquals(actualOutput, targetOutput) {
console.assert(
actualOutput === targetOutput,
`Expected ${actualOutput} to equal ${targetOutput}`
);
}
// Acceptance criteria:
// Proper Fraction check:
// Input: numerator = 2, denominator = 3
// target output: true
// Explanation: The fraction 2/3 is a proper fraction, where the numerator is less than the denominator. The function should return true.
const properFraction = isProperFraction(2, 3);
assertEquals(properFraction, true);
// Improper Fraction check:
// Input: numerator = 5, denominator = 2
// target output: false
// Explanation: The fraction 5/2 is an improper fraction, where the numerator is greater than or equal to the denominator. The function should return false.
const improperFraction = isProperFraction(5, 2);
assertEquals(improperFraction, false);
// Negative Fraction check:
// Input: numerator = -4, denominator = 7
// target output: true
const negativeFraction = isProperFraction(-4, 7);
// ====> complete with your assertion
assertEquals(negativeFraction, true);
// Equal Numerator and Denominator check:
// Input: numerator = 3, denominator = 3
// target output: false
const equalFraction = isProperFraction(3, 3);
// ====> complete with your assertion
assertEquals(equalFraction, false);
// Stretch:
// What other scenarios could you test for?
// Zero Numerator check:
// Input: numerator = 0, denominator = 5
// Target output: false
const zeroNumerator = isProperFraction(0, 5);
assertEquals(zeroNumerator, false);
// Negative Denominator check:
// Input: numerator = 3, denominator = -5
// Target output: true
const negativeDenominator = isProperFraction(3, -5);
assertEquals(negativeDenominator, true);
// Both Negative check:
// Input: numerator = -2, denominator = -3
// Target output: true
const bothNegative = isProperFraction(-2, -3);
assertEquals(bothNegative, true);
// Zero Denominator check:
// Input: numerator = 1, denominator = 0
// Target output: false
// Explanation: Division by zero is invalid; fraction cannot be proper.
const zeroDenominator = isProperFraction(1, 0);
assertEquals(zeroDenominator, false);
// Float Numerator check:
// Input: numerator = 2.5, denominator = 3
// Target output: false
// Explanation: Fractions with a non-integer numerator are not considered proper fractions.
const floatNumerator = isProperFraction(2.5, 3);
assertEquals(floatNumerator, false);
// Float Denominator check:
// Input: numerator = 2, denominator = 3.5
// Target output: false
// Explanation: Fractions with a non-integer denominator are invalid and should not be considered proper fractions.
const floatDenominator = isProperFraction(2, 3.5);
assertEquals(floatDenominator, false);
// Both Numerator and Denominator as Floats check:
// Input: numerator = 1.5, denominator = 2.5
// Target output: false
// Explanation: Fractions with both numerator and denominator as floats are invalid and should not be considered proper fractions.
const floatBoth = isProperFraction(1.5, 2.5);
assertEquals(floatBoth, false);
// Large Number check:
// Input: numerator = 99, denominator = 100
// Target output: true
// Explanation: 99/100 is a valid proper fraction, even with large values.
const largeNumbers = isProperFraction(99, 100);
assertEquals(largeNumbers, true);
// Negative Zero check:
// Input: numerator = -0, denominator = 5
// Target output: false
// Explanation: Negative zero is equivalent to zero; not a proper fraction.
const negativeZero = isProperFraction(-0, 5);
assertEquals(negativeZero, false);
// Denominator less than -1 check:
// Input: numerator = 1, denominator = -1
// Target output: false
// Explanation: Equal magnitude makes it improper, regardless of sign.
const negOneDenominator = isProperFraction(1, -1);
assertEquals(negOneDenominator, false);
// Proper Fraction check:
// Input: numerator = 2, denominator = 4
// Target output: true
// Explanation: 2/4 is a proper fraction because the absolute value of the numerator is less than the absolute value of the denominator.
const twoOverFour = isProperFraction(2, 4);
assertEquals(twoOverFour, true);