JavaScript For循环数组迭代问题-使用一个循环与两个循环
JavaScript For Loop Array Iteration Issue - Using One vs Two Loops
这个问题的目的是遍历列表,找到列表中最大的值,然后报告最高值的索引值。我能够使用两个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);
我最初试图只使用一个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];
highscoreSolutions.push(i);
}
}
console.log(highscore);
console.log(highscoreSolutions);
我不知道如何绕过添加0索引值的问题(而不必使用两个单独的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 = [];
var score;
for (var i = 0; i < scores.length; i++) {
score = scores[i];
if (score == highscore) {
highscore = score;
highscoreSolutions.push(i);
} else if (score > highscore) {
highscore = score;
// We have a new highest score, so all the values currently in the array
// need to be removed
highscoreSolutions = [i];
}
}
snippet.log(highscore);
snippet.log(highscoreSolutions.join(", "));
<!-- Script provides the `snippet` object, see http://meta.stackexchange.com/a/242144/134069 -->
<script src="http://tjcrowder.github.io/simple-snippets-console/snippet.js"></script>
对你来说,这听起来可能是过度设计的,但当你似乎在学习JavaScript时,我建议你跳过for
迭代(好吧,你应该知道它们存在,并且可以用来循环(,学习将JavaScript与函数式编程范型结合使用(这是一个很好的交互式教程(,因为它会带来更可读、更少出错的代码。
问题的解决方案可以使用[].reduce()
:
function highest(numbers) {
return numbers.reduce(function(winner, current, index) {
if (current > winner.value) {
return {value: current, indices: [index]};
} else if (current === winner.value) {
winner.indices.push(index);
return winner;
} else {
return winner;
}
}, {value: Number.NEGATIVE_INFINITY, indices: []});
}
function highest(numbers) {
return numbers.reduce(function(winner, current, index) {
if (current > winner.value) {
return {value: current, indices: [index]};
} else if (current === winner.value) {
winner.indices.push(index);
return winner;
} else {
return winner;
}
}, {value: Number.NEGATIVE_INFINITY, indices: []});
}
document.querySelector('button').addEventListener('click', function() {
var randoms = new Array(100).join(' ').split('').map(function() {return Math.ceil(Math.random() * 100)});
document.querySelector('#array').innerHTML = JSON.stringify(randoms);
document.querySelector('#result').innerHTML = JSON.stringify(highest(randoms));
});
<button>run</button>
<h3>the array</h3>
<pre id="array"></pre>
<h3>the result</h3>
<pre id="result"></pre>
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];
//clone array keeping original as scores, sort the new cloned array, grab the max value
var highscore = scores.slice(0).sort()[scores.length - 1];
var highscoreSolutions = [];
//find occurances of highscore and push to array
for (var i = 0; i < scores.length; i++){
if (scores[i] == highscore){
highscoreSolutions.push(i);
}
}
alert('highscore: ' + highscore + ''nindexes: ' + highscoreSolutions)
您可以使用array.slice(0(克隆阵列
然后运行sort((,通过检查长度-1 获取新排序数组的最后一个值
然后迭代循环以找到高核心所在的索引。
相关文章:
- 基于两个条件退出While循环
- 将循环中的两个文本框相乘,并在第三个文本框上显示结果
- 一个ajax循环有两个输出错误innerHTML
- 从两个基于0的for循环迭代器中获取单个基于0的索引的公式
- 两个循环JS函数相互冲突
- 识别两个文本中的单词(if 语句在 for 循环中)
- 如何在 JavaScript 中减少两个循环以提高效率
- 无法从 .each 循环加载谷歌地图上的两个标记
- 如何完成此循环?连接前面的两个字符
- 同时换掉两个Div,换成另一个Div并保持循环一段固定的秒数
- 两个不同的循环-都是don'不起作用
- 两个代码之间的区别是什么(javascript的循环)
- 一次两个对象的 JQuery 淡入淡出循环
- 是否可以让 DUST 模板循环和交错来自两个不同数组的元素
- 在 javascript 函数 javascript 动画中使用两个 for 循环
- 两个循环性能差异.交换内环和外环
- 具有两个条件的 while 循环仅检查一个
- 虽然循环多个条件不会同时检查这两个条件
- 同时循环两个jQuery对象
- 需要循环两个循环,并在内部循环中使用延迟迭代