Skip to content

Commit 868c5b9

Browse files
committed
fixed findMax function
1 parent d5f3e06 commit 868c5b9

File tree

2 files changed

+39
-1
lines changed

2 files changed

+39
-1
lines changed

Sprint-1/implement/max.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,12 @@
11
function findMax(elements) {
2+
const filteredArr = elements.filter(x => typeof x === 'number');
3+
4+
if (filteredArr.length == 0) {
5+
return -Infinity;
6+
}
7+
8+
// using 'spread sytax'
9+
return Math.max(...filteredArr);
210
}
311

412
module.exports = findMax;

Sprint-1/implement/max.test.js

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,28 +16,58 @@ 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+
20+
test("returns -Infinity for an empty array", () => {
21+
expect(findMax([])).toEqual(-Infinity);
22+
})
2023

2124
// Given an array with one number
2225
// When passed to the max function
2326
// Then it should return that number
2427

28+
test("given an array with one number, should return that number", () => {
29+
expect(findMax([8])).toEqual(8);
30+
expect(findMax([-5])).toEqual(-5);
31+
expect(findMax([0])).toEqual(0);
32+
})
33+
2534
// Given an array with both positive and negative numbers
2635
// When passed to the max function
2736
// Then it should return the largest number overall
2837

38+
test("return the largest number overall", () => {
39+
expect(findMax([-8, -4, 0, 4, 8])).toEqual(8);
40+
expect(findMax([-3, -2, -1, 4, 2, 3])).toEqual(4);
41+
})
42+
2943
// Given an array with just negative numbers
3044
// When passed to the max function
3145
// Then it should return the closest one to zero
3246

47+
test("given an array with only negative numbers, should return closest to 0", () => {
48+
expect(findMax([-2, -4, -1, -3, -100])).toEqual(-1);
49+
})
50+
3351
// Given an array with decimal numbers
3452
// When passed to the max function
3553
// Then it should return the largest decimal number
3654

55+
test("an array with decimal numbers, should return the largest decimal number", () => {
56+
expect(findMax([0.1, 0.2, 0.9, 0.8, 0.3, 0.7, 0.4, 0.6])).toEqual(0.9);
57+
})
58+
3759
// Given an array with non-number values
3860
// When passed to the max function
3961
// Then it should return the max and ignore non-numeric values
4062

63+
test("ignore the non-numeric values", () => {
64+
expect(findMax(["Blue", 3, "White", "Orange", "Pink"])).toEqual(3);
65+
})
66+
4167
// Given an array with only non-number values
4268
// When passed to the max function
4369
// Then it should return the least surprising value given how it behaves for all other inputs
70+
71+
test("an array with only non-number values", () => {
72+
expect(findMax(["Blue", "White", "Orange", "Pink"])).toEqual(-Infinity);
73+
})

0 commit comments

Comments
 (0)