【发布时间】:2020-08-16 21:22:22
【问题描述】:
在下面的代码中,我使用“push”来填充和清空数组。我需要帮助来编写上面的代码,以便它以相同的方式出现,而不使用“push”。 《Head First JavaScript Programming》一书向我提出了挑战。我试过了,但我很难过。
let 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 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]}`)
【问题讨论】:
-
filter+map? -
只需使用
bestSolutions[bestSolutions.length] = [i](或相同的计数器)
标签: javascript arrays push