简体   繁体   English

javascript如何parseInt数组的值以将它们全部求和

[英]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.

相关问题 如何对 JavaScript 中具有相同值的数组求和? - How to sum up array with same values in JavaScript? javascript总结数组中的值而不是数组中的所有值 - javascript sum up value in array and not all values in array 在 Javascript 中,如何从下拉列表和表格中汇总数组中的所有值? - In Javascript, How to sum up all the values in an array from a drop down list and form? 如何总结一个嵌套的Javascript对象数组的所有叶节点? - How to sum up all the leaf nodes of a nested Javascript object array? 如何将所有值放入数组并使用javascript获取它们 - How to push all values into array and get them with javascript parseInt关于数组和求和的问题 - parseInt on array and sum issue 如何对 JavaScript 中的内部数组表的一列中的所有值求和? - How to sum all the values in one column of an inner array table in JavaScript? 如何对多维数组 JavaScript 或 jquery 中的所有列值求和 - How to sum all column values in multi dimensional array JavaScript or jquery 如何获取具有多个键的对象数组中所有键的值,然后将它们全部相加[暂停] - How to get values of all keys in array of objects with more than one keys and then sum of them all [on hold] Javascript 嵌套数组:将项目减半并求和 - Javascript nested array: halve items and sum them up
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM