[英]javascript how to parseInt the values of an array to sum them all up
How do I parseInt the numbers in an array to add them up. 如何解析数组中的数字以将它们相加。 Where would I add the parseInt?
我将在哪里添加parseInt? SO i can then take the arrays summed values and divide by the .length value of the array.
这样,我就可以将数组的总和除以数组的.length值。 Would it be done in where the array is first stated or in the function for the sum?
会在首先声明数组的地方还是在求和函数中完成? It has not been working for me
它没有为我工作
<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>
Like this: 像这样:
sum=0;
for (i=1; i<randomArray.length; i++){
sum += parseInt(randomArray[i]);
}
result = sum / randomArray.length;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.