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 completed 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?