gpt4 book ai didi

javascript 如何解析数组的值以求和

转载 作者:行者123 更新时间:2023-12-03 04:30:46 24 4
gpt4 key购买 nike

如何解析数组中的数字以将它们相加。我应该在哪里添加 parseInt ?所以我可以将数组求和值除以数组的 .length 值。是在首先声明数组的地方还是在求和的函数中完成?它对我不起作用

<script>
var studentArr = new Array();
var scoreArr = new Array();

function enter() {

var studentName = document.getElementById("inp").value;
studentArr.push(studentName);

var stuval = "";

for(i=0; i < studentArr.length; i++)
{
stuval = stuval + studentArr[i] + "<br/>";
}

document.getElementById("iop").innerHTML = stuval;

var studentScore = document.getElementById("inps").value;
scoreArr.push(studentScore);

var scoreval = "";

for(i=0; i < scoreArr.length; i++)
{
scoreval = scoreval + scoreArr[i] + "<br/>";
}



}

function summ() {

var grade;
var average;
var sum = parseInt(scoreArr.valueOf());

if (scoreArr.valueOf() == 70) {

grade = "C";
document.getElementById("opo").innerHTML = grade;
}

if (scoreArr.valueOf() == 80) {

grade = "B";
document.getElementById("opo").innerHTML = grade;
}

if (scoreArr.valueOf() == 90) {

grade = "A";
document.getElementById("opo").innerHTML = grade;
}

scoreArr.sort(function(a, b){return b-a});
document.getElementById("hop").innerHTML = "Max Score is: <br>" + scoreArr[0];

scoreArr.sort(function(a, b){return a-b});
document.getElementById("lop").innerHTML = "Min Score is: <br>" + scoreArr[0];

average = sum / scoreArr.length;
document.getElementById("aop").innerHTML = average;

}

</script>

最佳答案

像这样:

sum=0;
for (i=1; i<randomArray.length; i++){
sum += parseInt(randomArray[i]);
}
result = sum / randomArray.length;

关于javascript 如何解析数组的值以求和,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43496885/

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