-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathquestion4.js
More file actions
41 lines (24 loc) · 814 Bytes
/
question4.js
File metadata and controls
41 lines (24 loc) · 814 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
38
39
40
41
//Question
//Find the second highest number in an integer array
//Answer
/*
My Method to find the second largest number in an array would be to sort the array in any order first,
i choose to sort it in ascending order and
then literally pick the second to last number
*/
let sampleArray = [134, 52, 23, 64];
function findSecondHighestNumber(arr) {
for (let i = 0; i <= arr.length; i++) {
for (x = 0; x < arr.length; x++) {
if (arr[i] < arr[x]) {
let old_Value_Of_Second = arr[x];
let old_Value_Of_First = arr[i];
arr[i] = old_Value_Of_Second;
arr[x] = old_Value_Of_First;
}
}
}
return arr[arr.length - 2];
}
let number = findSecondHighestNumber(sampleArray);
console.log(number);