计算长度为100的数组的前十个元素之和

<script>
    var arr = [];
    for(let i=0;i<100;i++)
    {
        arr[i] = parseInt(Math.random()*100);
    }
    var sum = arr.slice(0,10).reduce(function(x,y){
        return x+y;
    });
    console.log(sum)
script>

有关reduce()方法的介绍见该页:
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/Reduce

你可能感兴趣的:(Data,Struct,and,algorithm)