javascript 如何对数组的值进行 parseInt 以将它们全部加起来
javascript how to parseInt the values of an array to sum them all up
如何解析数组中的数字以将它们相加。我应该在哪里添加 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;
如何解析数组中的数字以将它们相加。我应该在哪里添加 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;