1/* The reduce() method executes a reducer function (that you provide) on each element of the array, resulting in
2 single output value.*/
3
4// Round all the numbers in an array, and display the sum:
5
6<button onclick="myFunction()">Get the Sum</button>
7<p>Sum of numbers in array: <span id="demo"></span></p>
8<script>
9var numbers = [15.5, 2.3, 1.1, 4.7];
10
11function getSum(total, num) {
12 return total + Math.round(num);
13}
14
15function myFunction(item) {
16 document.getElementById("demo").innerHTML = numbers.reduce(getSum, 0);
17}
18</script>
19