Skip to content

Commit aed6d3a

Browse files
committed
fixed the function
1 parent 8958df6 commit aed6d3a

4 files changed

Lines changed: 36 additions & 1 deletion

File tree

Sprint-1/implement/max.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,18 @@
11
function findMax(elements) {
2+
if (elements.length === 0) {
3+
return -Infinity;
4+
}
5+
6+
let max = -Infinity;
7+
8+
for (const item of elements) {
9+
if (typeof item === "number" && item > max) {
10+
max = item;
11+
}
12+
}
13+
14+
return max;
215
}
316

417
module.exports = findMax;
18+

Sprint-1/implement/max.test.js

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,28 +16,49 @@ const findMax = require("./max.js");
1616
// When passed to the max function
1717
// Then it should return -Infinity
1818
// Delete this test.todo and replace it with a test.
19-
test.todo("given an empty array, returns -Infinity");
19+
test("given an empty array, returns -Infinity", () => {
20+
expect(findMax([])).toBe(-Infinity);
21+
});
2022

2123
// Given an array with one number
2224
// When passed to the max function
2325
// Then it should return that number
26+
test("given an array with one number, it returns that number", () => {
27+
expect(findMax([7])).toBe(7);
28+
});
2429

2530
// Given an array with both positive and negative numbers
2631
// When passed to the max function
2732
// Then it should return the largest number overall
33+
test("given positive and negative numbers, it returns the largest number", () => {
34+
expect(findMax([-5, 10, -2, 7])).toBe(10);
35+
});
2836

2937
// Given an array with just negative numbers
3038
// When passed to the max function
3139
// Then it should return the closest one to zero
40+
test("given an array with just negative numbers, it returns the closest to zero", () => {
41+
expect(findMax([-10, -3, -20])).toBe(-3);
42+
});
3243

3344
// Given an array with decimal numbers
3445
// When passed to the max function
3546
// Then it should return the largest decimal number
47+
test("given an array with decimal numbers, it returns the largest decimal number", () => {
48+
expect(findMax([1.2, 3.5, 2.8])).toBe(3.5);
49+
});
3650

3751
// Given an array with non-number values
3852
// When passed to the max function
3953
// Then it should return the max and ignore non-numeric values
54+
test("given an array with non-number values, it returns the max and ignores non-numeric values", () => {
55+
expect(findMax(["hi", 10, "hello", 60, false, 20])).toBe(60);
56+
});
4057

4158
// Given an array with only non-number values
4259
// When passed to the max function
4360
// Then it should return the least surprising value given how it behaves for all other inputs
61+
test("given an array with only non-number values, it returns -Infinity", () => {
62+
expect(findMax(["a", "hello", false, null, undefined])).toBe(-Infinity);
63+
});
64+

Sprint-1/jest

Whitespace-only changes.

Sprint-1/week-1@1.0.0

Whitespace-only changes.

0 commit comments

Comments
 (0)