> For the complete documentation index, see [llms.txt](https://lochiwei.gitbook.io/web/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://lochiwei.gitbook.io/web/js/val/builtin/arr/ext/arr.sum-.average.md).

# arr.sum(), .average()

[JS](/web/js.md) ⟩ [objects](/web/js/val/obj.md) ⟩ [Array](/web/js/val/builtin/arr.md) ⟩ [methods](/web/js/val/builtin/arr/method.md) ⟩ sum(), average()

{% tabs %}
{% tab title="💾 程式" %}
:floppy\_disk: replit: [arr.sum()](https://replit.com/@pegasusroe/arrsum#index.js)

```javascript
// 🔸 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;
};
```

💈範例：

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

{% endtab %}

{% tab title="📘 手冊" %}

* [Array.prototype.reduce()](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/reduce)
  {% endtab %}

{% tab title="🗣 討論" %}

* [How to find the sum of an array of numbers](https://stackoverflow.com/questions/1230233/how-to-find-the-sum-of-an-array-of-numbers)
  {% endtab %}

{% tab title="⬇️ 應用" %}

* [國中成績一覽表](/web/appendix/gas/projects/guo-zhong-cheng-ji-yi-lan-biao.md)
  {% endtab %}
  {% endtabs %}
