简体   繁体   English

如何返回数组中所有值的总和?

[英]How do I return the sum of all values in an array?

I am very new to javascript and am having a problem figuring out if I should use return or document.write or both to return the sum of all values in an array called 'amount'.我对 javascript 非常陌生,并且在确定是否应该使用returndocument.write或两者来返回名为“amount”的数组中所有值的总和时遇到问题。 I am supposed to create a function names amountTotal().我应该创建一个函数名称 amountTotal()。

The purpose of which is to return the sum of all values in the amount array.其目的是返回amount数组中所有值的总和。 Then I am to declare a variable named total , setting its initial value to 0. Then create a for loop that loops through all the values in the amount array.然后我要声明一个名为total的变量,将其初始值设置为 0。然后创建一个 for 循环,循环遍历amount数组中的所有值。

At each iteration of the loop, add the current value of the array item to the value of the total variable.在循环的每次迭代中,将数组项的当前值与total变量的值相加。 Finally, after the loop is completed I need to return the value of the total variable.最后,循环完成后,我需要返回total变量的值。 The largest array value is [34] .最大的数组值为[34] This value will be written to a table called Summary该值将被写入一个名为Summary的表中

This is what I have written so far.这是我到目前为止所写的。

<script type="text/javascript">
    function amountTotal() {
        var total = 0;
        for (i = 0; i < 35; i++) {
            document.write("<td>" + i + "</td>")
        }
    }
</script>

Am I on the right track?我在正确的轨道上吗?

Return a value from function.从函数返回一个值。

function amountTotal(amount) {
        var total = 0;
        for (i = 0; i < amount.length; ++i) {
             total += amount[i]; // add each element in an array to total
        }
        return total;// return sum of elements in array
}
function sumArray(arr){
    var total = 0;
    arr.forEach(function(element){
        // 'element' in the parenthesis can be any name
        total += element;
    });
    return total;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 循环遍历数组并返回所有值的总和 - Loop through array and return sum of all values 我尝试编写一个函数,该函数将返回等于数组中传递的数字的所有数组元素的总和。 我该怎么做? - I try to write a function that will return the sum of all array elements that are equal to the number passed in the array. How can I do it? 使用Crossfilter,如何返回特定类型的所有id值的数组 - With Crossfilter how do I return an array of all the id values for a specific type 我如何在数组中找到相同的元素及其值的总和 - How do I find same element in an array and sum of their values 如何在异步函数中对 Javascript 数组的值求和? - How do I sum the values of a Javascript array in an asynchronous function? 我想要一个总和数组的 8 个值的所有结果 - I want a for that sum all the results of the 8 values of the array ZCCADCDEDB567ABAE643E15DCF0974E503Z - 如何在数组中添加和返回值的总和 - Mongoose - How to add and return a sum of values in an array 如何使用Angular对所有数组值求和 - How to sum all array values using Angular 那么如何将来自 react redux 的数组中所有对象的所有值相加? - So how can I sum all the values in all objects in an array which is coming from react redux? 如何将所有输入的新数组作为字符串返回? - How do I return a new array of all the inputs as strings?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM