-
Notifications
You must be signed in to change notification settings - Fork 0
/
test15.js
25 lines (22 loc) · 955 Bytes
/
test15.js
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
// write a js program to get the largest even number from an array of integers
// ====================================== first method < basic > =======================================
var getLargestEvenNumber = (arr) => {
var newArr = []
for (let i = 0; i < arr.length; i++) {
if (arr[i] % 2 == 0) {
newArr.push(arr[i])
}
}
newArr.sort;
return newArr[newArr.length - 1]
}
// ========================================= second method < using math.max > ==============================
var getLargestEvenNumber = (arr) =>
Math.max(...arr.filter(x => x % 2 == 0))
console.log(getLargestEvenNumber([1, 2, 3, 4, 5, 6, 7, 11, 123]))
// ========================================= third method < using find > ==============================
var getLargestEvenNumber = (arr) => {
arr.sort((a, b) => b - a)
return arr.find(x => x % 2 == 0)
}
console.log(getLargestEvenNumber([1, 2, 3, 4, 5, 6, 7, 11, 123]))