admin管理员组文章数量:1353265
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'. I am supposed to create a function names amountTotal().
The purpose of which is to return the sum of all values in the amount
array. 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.
At each iteration of the loop, add the current value of the array item to the value of the total
variable. Finally, after the loop is pleted I need to return the value of the total
variable. The largest array value is [34]
. This value will be written to a table called 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?
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'. I am supposed to create a function names amountTotal().
The purpose of which is to return the sum of all values in the amount
array. 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.
At each iteration of the loop, add the current value of the array item to the value of the total
variable. Finally, after the loop is pleted I need to return the value of the total
variable. The largest array value is [34]
. This value will be written to a table called 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?
Share Improve this question edited Sep 10, 2013 at 14:25 Moazzam Khan 3,1702 gold badges22 silver badges37 bronze badges asked Sep 10, 2013 at 14:15 Chris CarterChris Carter 151 gold badge1 silver badge6 bronze badges 6-
4
At this stage in your learning process, it's safe to just never even consider using
document.write()
. Ever. – Pointy Commented Sep 10, 2013 at 14:17 -
1
You should use
return
according to me. – Arpit Commented Sep 10, 2013 at 14:18 - 1 @Pointy I am also beginner to this language. Can you tell me,why not safe? I am asking out of curiosity. – Arup Rakshit Commented Sep 10, 2013 at 14:21
- So the code would read... return[i]? – Chris Carter Commented Sep 10, 2013 at 14:23
-
1
@ChrisCarter
document.write()
is not really part of JavaScript. It's a browser operation that's really only useful in a few special cases. – Pointy Commented Sep 10, 2013 at 15:03
2 Answers
Reset to default 4Return 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;
}
本文标签: javascriptHow do I return the sum of all values in an arrayStack Overflow
版权声明:本文标题:javascript - How do I return the sum of all values in an array? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1743892003a2557091.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论