Jumlah terbesar kedua dalam array javascript
['20','120','111','215','54','78'].sort(function(a, b) { return b - a; })[1];
// '120'
Important Impala
['20','120','111','215','54','78'].sort(function(a, b) { return b - a; })[1];
// '120'
const data = [30, 8, 2, 350, 4, 63, 98];
let max = 0;
var nth = 5;
for (let i = 0; i < nth; i++) {
max = 0;
for (let j = 0; j < data.length; j++) {
if (data[j] > max) {
max = data[j];
}
}
var ind = data.indexOf(max);
console.log(data);
const key = data.slice(ind, ind + 1);
}
console.log(nth + " largest number is ", max);