-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcalc.ts
59 lines (51 loc) · 1.44 KB
/
calc.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
export {};
declare global {
/* eslint-disable-next-line @typescript-eslint/no-unused-vars */
interface Array<T = number> {
/**
* [拡張メソッド]
* 数値配列の合計値を取得します。
* @return 合計値
*/
sum(): number;
/**
* [拡張メソッド]
* 数値配列の平均値を取得します。
* @return 平均値
*/
average(): number;
/**
* [拡張メソッド]
* 数値配列の最大値を取得します。
* @return 最大値
*/
max(): number;
/**
* [拡張メソッド]
* 数値配列の最小値を取得します。
* @return 最小値
*/
min(): number;
}
}
Array.prototype.sum = function () {
const items = this as number[];
if (!Array.isArray(items)) return 0;
return items.reduce((sum, num) => sum + num, 0);
};
Array.prototype.average = function () {
const items = this as number[];
if (!Array.isArray(items) || items.length === 0) return 0;
const total = items.reduce((sum, num) => sum + num, 0);
return total / items.length;
};
Array.prototype.max = function () {
const items = this as number[];
if (!Array.isArray(items)) return 0;
return items.reduce((max, num) => (max > num ? max : num), 0);
};
Array.prototype.min = function () {
const items = this as number[];
if (!Array.isArray(items) || items.length === 0) return 0;
return items.reduce((min, num) => (min < num ? min : num));
};