Javascript examples for Array:reduceRight
Subtract the numbers, right-to-left, and display the sum:
<!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>