此问题的目的是迭代列表,找到列表中的最大值,然后报告最高值的索引值.我能够使用两个for循环来解决这个问题:
var scores = [60, 50, 58, 54, 54, 58, 50, 52, 54, 48, 69, 34, 55, 51, 52, 44, 51, 69, 64, 66, 55, 52, 44, 18, 41, 53, 55, 61, 51, 44];
var highscore = 0;
var highscoreSolutions = [];
for (var i = 0; i < scores.length; i++){
if (scores[i] > highscore){
highscore = scores[i];
}
}
for (var i = 0; i < scores.length; i++){
if (scores[i] == highscore){
highscoreSolutions.push(i);
}
}
console.log(highscore);
console.log(highscoreSolutions);
Run Code Online (Sandbox Code Playgroud)
我最初尝试使用一个for循环来解决这个问题,但是我遇到了一个类似的初始化问题,也就是说,无论如何,第一个索引值将包含在最高分数列表中:
var …Run Code Online (Sandbox Code Playgroud)