从数组中获取最大的两个变量值和名称



我正试图找出如何从一组数据中获得两个最高变量和变量名称。以下是我到目前为止为获得第一名所做的努力。

var one = 5
var two = 6
var three = 5
var four = 10
var five = 2

(或作为数组:var array = [one, two, three, four, five]

if (one > two && three && four && five) { 
console.log('One Highest')
 }

if (two > one && three && four && five) { 
        console.log('Two Highest')
         }
if (three > one && two && four && five) { 
        console.log('Three Highest')
         }
 if (four > one && three && two && five) { 
            console.log('Four Highest')
          }
 if (five > one && two && four && three) { 
            console.log('Five Highest')
          }

一旦我知道哪一个是最高的,我需要能够根据获胜者添加点到另一个变量…

的例子:

if var one is the greatest value {
 // add points to var one items
 thingOne +50
 thingTwo +50
}
if var two is the greatest value {
 // add points to var two items
 thingThree +50
 thingFour +50
}

如果可能的话,我也试图能够找到第二高的分数和var名称。

谢谢你的建议!

我会试一试,但不确定。这是我第一次尝试回答问题。我猜这是JavaScript。不确定这是否正确

array myArray[] = {4, 6, 8, 3, 8, 7};//Array like this no ?
var firstHighest = array[0];//We say any of them is highest
var secondHighest = array[0];//We say any of them is second highest
int x;//for loop 
for(x=0;x<=array.length;x++){
if(array[x] > firstHighest){
firstHighest = array[x];
}
} 
//After this we have highest I think
//Now lets get second Highest
for(x=0;x<=array.length;x++){
if((array[x] > secondHighest)&&(array[x] != firstHighest)){
secondHighest = array[x];
}
} 
//We now have second highest i think
//We can now use them two variables
//I pretty sure there are functions that do this for you though

最新更新