Skip to content

Commit 411b770

Browse files
committed
numeric string coverage applied to function and test cases
1 parent 989bcb6 commit 411b770

2 files changed

Lines changed: 23 additions & 3 deletions

File tree

Sprint-1/implement/max.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
function findMax(elements) {
2-
const numbers = elements.filter((el) => typeof el === "number");
2+
if (!Array.isArray(elements)) {
3+
return -Infinity;
4+
}
5+
6+
const numbers = elements.filter((el) => Number.isFinite(el));
37

48
if (numbers.length === 0) {
59
return -Infinity;

Sprint-1/implement/max.test.js

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,8 @@ describe("findMax", () => {
3939
// Given an array with non-number values
4040
// When passed to the max function
4141
// Then it should return the max and ignore non-numeric values
42-
test("ignores non-number values", () => {
43-
expect(findMax(["hey", 10, "hi", 60, 10])).toBe(60);
42+
test("ignores non-number values (including numeric strings)", () => {
43+
expect(findMax(["hey", 10, "300", 60, 10])).toBe(60);
4444
});
4545

4646
// Given an array with only non-number values
@@ -49,4 +49,20 @@ describe("findMax", () => {
4949
test("returns -Infinity when array has only non-number values", () => {
5050
expect(findMax(["a", "b", "c"])).toBe(-Infinity);
5151
});
52+
53+
test("does not treat numeric strings as numbers", () => {
54+
expect(findMax([20, "300"])).toBe(20);
55+
});
56+
57+
test("returns -Infinity when only numeric strings are present", () => {
58+
expect(findMax(["100", "200", "300"])).toBe(-Infinity);
59+
});
60+
61+
test("ignores NaN and Infinity values", () => {
62+
expect(findMax([10, NaN, 50, Infinity, -Infinity])).toBe(50);
63+
});
64+
65+
test("returns -Infinity when only NaN and Infinity are present", () => {
66+
expect(findMax([NaN, Infinity, -Infinity])).toBe(-Infinity);
67+
});
5268
});

0 commit comments

Comments
 (0)