Skip to content

Commit 0c123a4

Browse files
committed
wrote the implemention and test cases for max.js in sprint-1
1 parent d7200c9 commit 0c123a4

File tree

2 files changed

+44
-1
lines changed

2 files changed

+44
-1
lines changed

Sprint-1/implement/max.js

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,26 @@
11
function findMax(elements) {
2+
if (!Array.isArray(elements)) {
3+
return null;
4+
}
5+
6+
// If the array is truly empty, return -Infinity
7+
if (elements.length === 0) {
8+
return -Infinity;
9+
}
10+
11+
// Filter only numeric values
12+
const numbers = elements.filter(function (item) {
13+
return typeof item === "number" && !isNaN(item);
14+
});
15+
16+
// If there are no numeric values but the array wasn't empty, return null
17+
if (numbers.length === 0) {
18+
return null;
19+
}
20+
21+
// Otherwise, return the largest number
22+
return Math.max.apply(null, numbers);
223
}
324

425
module.exports = findMax;
26+

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+
});
22+
2023

2124
// Given an array with one number
2225
// When passed to the max function
2326
// Then it should return that number
27+
test("given an array with one number, returns that number", () => {
28+
expect(findMax([5])).toBe(5);
29+
});
2430

2531
// Given an array with both positive and negative numbers
2632
// When passed to the max function
2733
// Then it should return the largest number overall
34+
test("given an array with both positive and negative numbers, returns the largest", () => {
35+
expect(findMax([30, 50, 10, 40])).toBe(50);
36+
});
2837

2938
// Given an array with just negative numbers
3039
// When passed to the max function
3140
// Then it should return the closest one to zero
41+
test("given an array with just negative numbers, returns the closest to zero", () => {
42+
expect(findMax([-10, -5, -20])).toBe(-5);
43+
});
3244

3345
// Given an array with decimal numbers
3446
// When passed to the max function
3547
// Then it should return the largest decimal number
48+
test("given an array with decimal numbers, returns the largest", () => {
49+
expect(findMax([3.14, 2.71, 1.41])).toBe(3.14);
50+
});
3651

3752
// Given an array with non-number values
3853
// When passed to the max function
3954
// Then it should return the max and ignore non-numeric values
55+
test("given an array with non-number values, returns the largest number", () => {
56+
expect(findMax(['hey', 10, 'hi', 60, 10])).toBe(60);
57+
});
4058

4159
// Given an array with only non-number values
4260
// When passed to the max function
4361
// Then it should return the least surprising value given how it behaves for all other inputs
62+
test("given an array with only non-number values, returns the least surprising value", () => {
63+
expect(findMax(['hey', 'hi', 'hello'])).toBe(null);
64+
});

0 commit comments

Comments
 (0)