问题描述
在下面的代码中,我使用“ push”来填充并清空数组。我需要编写上述代码的帮助,以便以相同的方式显示出来,而无需使用“ push”。 “ Head First JavaScript Programming”一书使我受到挑战。我已经尝试过,但是很沮丧。
let scores = [60,50,60,58,54,52,48,69,34,55,51,44,64,66,61,46,31,57,18,41,53,44
]
var highscore = 0
var output
for (var i = 0; i < scores.length; i++) {
output = `Bubbles solution # ${i} score: ${scores[i]}<br>`
document.write(output)
if (scores[i] > highscore) {
highscore = scores[i]
}
}
let bestSolutions = []
for (var i = 0; i < scores.length; i++) {
if (scores[i] == highscore) {
bestSolutions.push([i])
}
}
document.write(`Bubbles Tests: ${scores.length}<br>`)
document.write(`Highest Bubble score: ${highscore}<br>`)
document.write(`Solutions with highest score: #${bestSolutions[0]} and #${bestSolutions[1]}`)
解决方法
如果我正确理解这一点,则您正在寻找一个数字数组中的所有最大值。在这种情况下,可以使用reduce
和filter
方法来代替使用push
。
function findMaxes(arr) {
const max = Math.max.apply(null,arr);
return arr.filter(n => n == max);
}
例如:
findMaxes([3,5,1,4,5]) == [5,5]
findMaxes([-1,-1,-1]) == [-1,-1]
如果要查找所有maxes的位置:
function findAllMaxPositions(arr) {
const max = Math.max.apply(null,arr);
return arr.map((e,index) => [e,index])
.filter(pair => pair[0] == max)
.map(e => e[1]);
}
,
好的,下面是一种方法:如果希望bestscores
数组包含scores
中等于highscore
的元素的索引,则可以定义{{1 }}作为bestcores
的 map ,它通过一个元组scores
,如果返回(value,index)
则返回index
,否则返回value == highscore
。
地图示例
null
一起在您的情况下
const a = [2,3,6];
const b = a.map(value => value * 2); // [4,6,10,12];
const c = a.map((value,index) => index); //[0,2,4];
const d = a.map((value,index) => value + index); //[2,7,10];
const e = a.map((value,index) => value >= 3 ? index : null);
// [null,null,4]. We can deal with those nulls with a filter
const f = e.filter(value => value != null) // [1,4]
这为您提供了一种高级方法,但是由于它实际上对const highscores = scores.map((value,index) => value == highscore ? index : null)
.filter(value => value != null);
数组进行了两次迭代,因此计算效率较低。实际上,对数组和scores
进行手动迭代会更有效。
您可以尝试这种方法:
- 使用
Math.max.apply
查找最高分数。 - 使用循环查找所有匹配值并存储其索引。
let scores = [ 60,50,60,58,54,52,48,69,34,55,51,44,64,66,61,46,31,57,18,41,53,44 ]
var highScore = Math.max.apply(null,scores)
var bestSolutions = scores.reduce((acc,score,index) =>
score === highScore ? [ ...acc,index] : acc,[]
);
document.write(`Bubbles Tests: ${scores.length}<br>`)
document.write(`Highest Bubble Score: ${highScore}<br>`)
document.write(`Solutions with highest score: #${bestSolutions[0]} and #${bestSolutions[1]}`)
基于算法:
-
创建2个变量
highScore
和bestSolutions
。 -
遍历数组。
-
对于每次迭代,请进行2次检查:
-
如果当前
score
大于highScore
- 如果是,请以当前分数初始化
highScore
,并以当前索引初始化bestSolutions
。
- 如果是,请以当前分数初始化
-
如果
highScore
等于当前的score
- 如果是,则将索引推送到当前
bestSolutions
- 如果是,则将索引推送到当前
let scores = [ 60,44 ];
let highScore = 0;
let bestSolutions = [];
for(let i = 0; i< scores.length; i++) {
if (scores[i] > highScore) {
highScore = scores[i];
bestSolutions = [ i ];
} else if (scores[i] === highScore) {
bestSolutions.push(i)
}
}
document.write(`Bubbles Tests: ${scores.length}<br>`)
document.write(`Highest Bubble Score: ${highScore}<br>`)
document.write(`Solutions with highest score: #${bestSolutions[0]} and #${bestSolutions[1]}`)