gpt4 book ai didi

javascript - while 循环。无法对 while 语句进行硬编码

转载 作者:行者123 更新时间:2023-11-28 04:40:18 25 4
gpt4 key购买 nike

function start() {
var output = "";
var index = 0;
var total = 0;
var average = 0;
var arr = []
while(arr.length < 12){
var randomnumber = Math.ceil(Math.random()*20)
if(arr.indexOf(randomnumber) > -1) continue;
arr[arr.length] = randomnumber;
}

output = output + "List of all values in the array: " + arr;
output = output + "<br/>" + "Total number of values in the array: " + arr.length + "<br/>";

while(index < arr.length) {
total = total + arr[index];
index++;
}
average = total / index;
output = output + "Total of all values: " + total + "<br/>";
output = output + "Average of all values: " + average;

document.getElementById("msg").innerHTML = output;
}

有人告诉我不允许对语句进行硬编码,如何更改“while”语句以便不进行硬编码?

最佳答案

可能他们的意思是

var arr =  new Array(12);
var randIndex=0;

while(randIndex < arr.length){
var randomnumber = Math.ceil(Math.random()*20)
if(arr.indexOf(randomnumber) > -1) continue;
arr[randIndex] = randomnumber;
randIndex++;
}

关于javascript - while 循环。无法对 while 语句进行硬编码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43845596/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com