Skip to content

Commit 4b1ad2d

Browse files
committed
adding tests and condition checks for different types of input
1 parent 6166064 commit 4b1ad2d

File tree

2 files changed

+71
-0
lines changed

2 files changed

+71
-0
lines changed

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,12 @@
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 numerator !== "string")
12+
return false;
13+
if (typeof denominator !== "number" && typeof denominator !== "string")
14+
return false;
15+
if (isNaN(numerator) || isNaN(denominator)) return false;
16+
if (denominator == 0) return false; // handle zero denominator case
1117
return Math.abs(numerator) < Math.abs(denominator);
1218
}
1319

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

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,3 +69,68 @@ test("should return false when 3.5 > 2.5", () => {
6969
test("should return false for non-numeric inputs", () => {
7070
expect(isProperFraction("a", 2)).toEqual(false);
7171
});
72+
73+
// Stretch 10: numerator is a string that can be converted to a number
74+
test("should return true when '2' < 3", () => {
75+
expect(isProperFraction("2", 3)).toEqual(true);
76+
});
77+
78+
// Stretch 11: denominator is a string that can be converted to a number
79+
test("should return false when 5 > '2'", () => {
80+
expect(isProperFraction(5, "2")).toEqual(false);
81+
});
82+
83+
// Stretch 12: both numerator and denominator are strings that can be converted to numbers
84+
test("should return true when '2' < '3'", () => {
85+
expect(isProperFraction("2", "3")).toEqual(true);
86+
});
87+
88+
// Stretch 13: numerator is null
89+
test("should return false when numerator is null", () => {
90+
expect(isProperFraction(null, 3)).toEqual(false);
91+
});
92+
93+
// Stretch 14: denominator is null
94+
test("should return false when denominator is null", () => {
95+
expect(isProperFraction(3, null)).toEqual(false);
96+
});
97+
98+
// Stretch 15: both numerator and denominator are null
99+
test("should return false when both numerator and denominator are null", () => {
100+
expect(isProperFraction(null, null)).toEqual(false);
101+
});
102+
103+
// Stretch 16: numerator is undefined
104+
test("should return false when numerator is undefined", () => {
105+
expect(isProperFraction(undefined, 3)).toEqual(false);
106+
});
107+
108+
// Stretch 17: denominator is undefined
109+
test("should return false when denominator is undefined", () => {
110+
expect(isProperFraction(3, undefined)).toEqual(false);
111+
});
112+
113+
// Stretch 18: both numerator and denominator are undefined
114+
test("should return false when both numerator and denominator are undefined", () => {
115+
expect(isProperFraction(undefined, undefined)).toEqual(false);
116+
});
117+
118+
// Stretch 19: numerator is an array
119+
test("should return false when numerator is an array", () => {
120+
expect(isProperFraction([2], 3)).toEqual(false);
121+
});
122+
123+
// Stretch 20: denominator is an array
124+
test("should return false when denominator is an array", () => {
125+
expect(isProperFraction(2, [3])).toEqual(false);
126+
});
127+
128+
// Stretch 21: numerator is an object
129+
test("should return false when numerator is an object", () => {
130+
expect(isProperFraction({ num: 2 }, 3)).toEqual(false);
131+
});
132+
133+
// Stretch 22: denominator is an object
134+
test("should return false when denominator is an object", () => {
135+
expect(isProperFraction(2, { den: 3 })).toEqual(false);
136+
});

0 commit comments

Comments
 (0)