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

时间:2016-11-16 22:20:33

标签: javascript jquery html arrays

我试图弄清楚如何从一组数据中获取两个最高变量和变量名称。这是我到目前为止试图获得第一高的时间。

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名称。

感谢您的任何建议!

1 个答案:

答案 0 :(得分:0)

我会去,但不确定。这是尝试回答问题的第一次尝试。我认为这是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