📜  sum range javascript 代码示例

📅  最后修改于: 2022-03-11 15:02:33.555000             🧑  作者: Mango

代码示例1
function sumAll(arr) {
    let fullArr = [];
    let sum = 0;
    const reducer = (accumulator, currentValue) => accumulator + currentValue;
    arr.sort(function(a, b) {
        return a - b
    });
    for (let i = arr[0]; i <= arr[1]; i++) {
        fullArr.push(i);
    }
    sum = fullArr.reduce(reducer);
    return sum;
}