-
-
Notifications
You must be signed in to change notification settings - Fork 283
Expand file tree
/
Copy pathmax.js
More file actions
22 lines (18 loc) · 870 Bytes
/
max.js
File metadata and controls
22 lines (18 loc) · 870 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
function findMax(elements) {
//find largest number in an array
if (!Array.isArray(elements)) {
throw new Error("Input must be an array"); //checks input is an array if not throws error
}
if (elements.length === 0) return -Infinity; // if empty then returns -Infinity
let max = -Infinity; //starts at -Infinity so any number will be bigger
let foundValid = false; //starts at false and will become true if valid number found
for (const val of elements) {
if (!Number.isFinite(val)) continue;
foundValid = true; // loops and checks if input valid and if" valid changes foundValid to true
if (val > max) {
max = val; // if bigger than max update max
}
}
return foundValid ? max : "Invalid Input"; // if at least one valid value is found return max if no valid number is found return Invalid Input
}
module.exports = findMax;