💾arr.sum(), .average()

JSobjectsArraymethods ⟩ sum(), average()

💾 replit: arr.sum()

// 🔸 arr.sum()
Array.prototype.sum = function(){
    return this.reduce((result, value) => result + value, 0);
};

// 🔸 arr.average()
Array.prototype.average = function(){
    return this.sum() / this.length;
};

💈範例:

[1,2,3].sum(),        // 6
[].sum(),             // 0
[1,2,3].average(),    // 2

Last updated