专注前端素材!
源代码:
点击运行
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>素材8网教程(study.sucai8.cn)</title> </head> <body> <p>点击按钮从右到左,由最后一个元素开始减去每个数组的元素。</p> <button onclick="myFunction()">点我</button> <p>计算后的值: <span id="demo"></span></p> <script> var numbers = [2, 45, 30, 100]; function getSum(total, num) { return total - num; } function myFunction(item) { document.getElementById("demo").innerHTML = numbers.reduceRight(getSum); } </script> </body> </html>
运行结果