【问题标题】:Get highest two variable values and name from array [duplicate]从数组中获取最高的两个变量值和名称[重复]
【发布时间】:2016-11-17 06:34:17
【问题描述】:

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

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

(或作为数组:var array = [一、二、三、四、五]

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

感谢您的建议!

【问题讨论】:

  • 数组在哪里
  • 试试Math.max...
  • @rassar 解决了值但是我如何获得相应的var名称?
  • 这听起来像是一个 XY 问题。你能描述一下你面临的整个问题吗?为什么要第二高分和 var 名称?
  • @AndrewLeonardi 为什么你首先将它们作为单独的变量来做?只需使用一个数组。

标签: javascript jquery html arrays


【解决方案1】:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-06-04
    • 2010-11-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多