简单教程
提交运行
代码编辑器:
<p>点击按钮计算数组元素的总和</p> <button onclick="myFunction()">点我</button> <p>数组元素总和: <span id="demo"></span></p> <script> var numbers = [65, 44, 12, 4]; function getSum(total, num) { return total + num; } function myFunction(item) { document.getElementById("demo").innerHTML = numbers.reduceRight(getSum); } </script>
运行结果: