Skip to content

Commit 0f21769

Browse files
committed
Finding the max value
1 parent 4ddb43b commit 0f21769

2 files changed

Lines changed: 31 additions & 3 deletions

File tree

‎Sprint-1/implement/max.js‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,14 @@
11
function findMax(elements) {
2+
// Filtering out non-numeric values
3+
const numericElements = elements.filter(element => typeof element === 'number' && !isNaN(element));
4+
5+
// If there are no numeric values, return -Infinity
6+
if (numericElements.length === 0) {
7+
return -Infinity;
8+
}
9+
10+
// Using Math.max to find the maximum value in the numeric elements
11+
return Math.max(...numericElements);
212
}
313

414
module.exports = findMax;

‎Sprint-1/implement/max.test.js‎

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,28 +16,46 @@ 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, when passed to the max function, then it should return -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
24-
26+
test ("Given an array with one number, when passed to the max function, then it should return that number", () => {
27+
expect(findMax([5])).toBe(5);
28+
});
2529
// Given an array with both positive and negative numbers
2630
// When passed to the max function
2731
// Then it should return the largest number overall
32+
test ("Given an array with both positive and negative numbers, when passed to the max function, then it should return the largest number overall", () => {
33+
expect(findMax([-10, 20, -30, 40, -50])).toBe(40);
34+
});
2835

2936
// Given an array with just negative numbers
3037
// When passed to the max function
3138
// Then it should return the closest one to zero
32-
39+
test ("Given an array with just negative numbers, when passed to the max function, then it should return the closest one to zero", () => {
40+
expect(findMax([-10, -20, -30, -40, -50])).toBe(-10);
41+
});
3342
// Given an array with decimal numbers
3443
// When passed to the max function
3544
// Then it should return the largest decimal number
45+
test ("Given an array with decimal numbers, when passed to the max function, then it should return the largest decimal number", () => {
46+
expect(findMax([1.5, 2.5, 3.5, 4.5, 5.5])).toBe(5.5);
47+
});
3648

3749
// Given an array with non-number values
3850
// When passed to the max function
3951
// Then it should return the max and ignore non-numeric values
52+
test ("Given an array with non-number values, when passed to the max function, then it should return the max and ignore non-numeric values", () => {
53+
expect(findMax(['hey', 10, 'hi', 60, 10])).toBe(60);
54+
});
4055

4156
// Given an array with only non-number values
4257
// When passed to the max function
4358
// Then it should return the least surprising value given how it behaves for all other inputs
59+
test ("Given an array with only non-number values, when passed to the max function, then it should return the least surprising value given how it behaves for all other inputs", () => {
60+
expect(findMax(['hello', 'world'])).toBe(-Infinity);
61+
});

0 commit comments

Comments
 (0)