Subtract the numbers, right-to-left, and display the sum: - Javascript Array

Javascript examples for Array:reduceRight

Description

Subtract the numbers, right-to-left, and display the sum:

Demo Code

ResultView the demo in separate window

<!DOCTYPE html>
<html>
<body>

<button onclick="myFunction()">Test</button>

<p>Sum of numbers in array: <span id="demo"></span></p>

<script>
var numbers = [2, 45, 30, 100];/*from w  w  w . j  a v  a  2 s  .  com*/

function getSum(total, num) {
    return total - num;
}
function myFunction(item) {
    document.getElementById("demo").innerHTML = numbers.reduceRight(getSum);
}
</script>

</body>
</html>

Related Tutorials