-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathSecondGreat.js
51 lines (40 loc) · 1.28 KB
/
SecondGreat.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
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
//return the second greatest low and high in the array,
// ex: [1,4,5,7,8,4]-> 4 7
//c1
//(1) delete the duplicated elements to get unique array
//(2) sort the array ascending order
//(3) return result
function SecondGreatLow(arr){
//(1)
let uniqueArr =arr.filter(function(element, index, arraySelf){return arraySelf.indexOf(element)===index});
console.log(uniqueArr);
//(2)
let newArr = uniqueArr.sort(function(a,b) { return a-b});
if(newArr.length ===2){
return newArr[0] + " " + newArr[1];
}else {
return newArr[1]+ " " + newArr[newArr.length-2];
}
};
console.log(SecondGreatLow([1,3,9, 8,5,7,4,3]));
// //c2
// function SecondGreatLow(arr) {
// // code goes here
// var sorted = arr.sort(function(a,b){return a-b;});
// var greatest = sorted[sorted.length - 1];
// var lowest = sorted[0];
// var secondGreatest, secondLowest;
// for (var i = 0; i < sorted.length; i++) {
// if (sorted[i] !== lowest) {
// secondLowest = sorted[i];
// break;
// }
// }
// for (var i = sorted.length - 1; i >= 0; i--) {
// if (sorted[i] !== greatest) {
// secondGreatest = sorted[i];
// break;
// }
// }
// return secondLowest + " " + secondGreatest;
// }