-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathindexMlArraymax.js
More file actions
37 lines (31 loc) · 902 Bytes
/
indexMlArraymax.js
File metadata and controls
37 lines (31 loc) · 902 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
import { isAnyArray } from './indexIsAnyArray.js';
export default function max(input, options = {}) {
if (!isAnyArray(input)) {
throw new TypeError('input must be an array');
}
if (input.length === 0) {
throw new TypeError('input must not be empty');
}
const { fromIndex = 0, toIndex = input.length } = options;
if (
fromIndex < 0 ||
fromIndex >= input.length ||
!Number.isInteger(fromIndex)
) {
throw new Error('fromIndex must be a positive integer smaller than length');
}
if (
toIndex <= fromIndex ||
toIndex > input.length ||
!Number.isInteger(toIndex)
) {
throw new Error(
'toIndex must be an integer greater than fromIndex and at most equal to length',
);
}
let maxValue = input[fromIndex];
for (let i = fromIndex + 1; i < toIndex; i++) {
if (input[i] > maxValue) maxValue = input[i];
}
return maxValue;
}