2016-11-16 77 views
-1

獲得最高兩位變量值和名字,我試圖找出如何從一組數據中得到最高的兩個變量,變量名。這是我爲了獲得第一名而迄今爲止所提出的。從陣列

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

(或作爲數組:var數組= [一,二,三,四,五]

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 
} 

如果可能的話我也想能夠找到SE cond最高分和var名稱。

感謝您的任何建議!

+2

哪裏陣列 – rlemon

+0

嘗試'Math.max' ... – rassar

+0

@rassar這解決的價值,但我怎麼得到相應的變量名稱? – AndrewLeonardi

回答

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