多得分最高的回报指数 [英] Return index of multiple highest scores

查看:126
本文介绍了多得分最高的回报指数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的工作如何从一个数组返回得分最高的指数的想法,但我在努力弄清楚,如果我能从此方法返回多个项目,或者我是否需要使用另一种方法返回数组中的两个最高分的索引。

  VAR成绩= [60,50,60,58,54,54,
                  58,50,52,54,48,69,
                  34,55,51,52,44,51,
                  69,64,66,55,52,61,
                  46,31,57,52,44,18,
                  41,53,55,61,51,44];VAR测试= scores.length;
VAR最高;
对于(VAR I = 0; I< scores.length;我++){
  如果(ⅰ=== 0){
    最高得分= [I]
  }
  如果(得分[Ⅰ]≥最高){
    最高得分= [I]
  }
}


解决方案

返回包含索引的数组。当你获得比最高的更高的元素,空数组,并把我进去。当你等于最高的元素,推我到阵列上。

  VAR最高得分= [0];
变种索引= [0];
对于(VAR I = 1; I< scores.length;我++){
    如果(得分[Ⅰ]≥最高){
        //找到了新的最高,一切复位
        指数= [I]
        最高得分= [I]
    }否则如果(得分[I] ==最高){
        //找到重复最高的,它的索引添加到索引数组
        indexes.push(ⅰ);
    }
}

I'm working on an idea of how to return the index of the highest scores from an array, but I'm struggling to figure out if I could return multiple items from this method, or whether I would need to use an alternative method to return the index of two highest scores in an array.

 var scores = [60, 50, 60, 58, 54, 54,
                  58, 50, 52, 54, 48, 69,
                  34, 55, 51, 52, 44, 51,
                  69, 64, 66, 55, 52, 61,
                  46, 31, 57, 52, 44, 18,
                  41, 53, 55, 61, 51, 44];

var tests = scores.length;
var highest;
for (var i = 0; i < scores.length; i++) {
  if (i === 0) {
    highest = scores[i];
  }
  if (scores[i] > highest) {
    highest = scores[i];
  }
}

解决方案

Return an array containing the indexes. When you get an element higher than the highest, empty the array and put i into it. When you get an element equal to the highest, push i onto the array.

var highest = scores[0];
var indexes = [0];
for (var i = 1; i < scores.length; i++) {
    if (scores[i] > highest) {
        // Found new highest, reset everything
        indexes = [i];
        highest = scores[i];
    } else if (scores[i] == highest) {
        // Found duplicate highest, add its index to indexes array
        indexes.push(i);
    }
}

这篇关于多得分最高的回报指数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆