I am currently learning JavaScript and facing a dilemma about whether to use return
, document.write
, or both in order to calculate the sum of all values in an array named 'amount'. I have been tasked with creating a function called amountTotal().
The main goal is to return the total sum of all values within the amount
array. To achieve this, I need to initialize a variable called total
with a starting value of 0. Subsequently, I should implement a for loop that iterates through each value in the amount
array.
During each iteration of the loop, I must add the current value of the array element to the existing value of the total
variable. When the loop completes, the final step involves returning the calculated total value. The largest value in the array is [34]
, which will be displayed in a table named Summary
.
This snippet shows my progress so far:
<script type="text/javascript">
function amountTotal() {
var total = 0;
for (i = 0; i < 35; i++) {
document.write("<td>" + i + "</td>")
}
}
</script>
Do you think I am heading in the right direction?