W3School TIY Editor
W3School 在线教程
改变方向
暗黑模式
运行代码
<!DOCTYPE html> <html> <body> <h1>JavaScript 数组</h1> <p>计算数组中被四舍五入的数字的总和。</p> <p id="demo"></p> <script> const numbers = [15.5, 2.3, 1.1, 4.7]; document.getElementById("demo").innerHTML = numbers.reduce(getSum, 0); function getSum(total, num) { return total + Math.round(num); } </script> </body> </html>