-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmean-median-mode.js
65 lines (54 loc) · 1.58 KB
/
mean-median-mode.js
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
60
61
62
63
64
65
//문제
//Given an array of numbers, calculate the mean, median, and mode.
//평균값 => 총합 / 전체개수
//중앙값 => 중앙에 있는 값
//최빈값 => 가장 많이 나온 수
(function () {
const array1 = [1, 2, 3, 4, 4, 5, 5];
const array2 = [1, 1, 2, 2, 3, 3, 4, 4];
function State(array) {
this.array = array;
}
State.prototype.getMean = function () {
const sum = this.array.reduce((ac, val) => ac + val, 0);
return Number((sum / this.array.length).toFixed(2));
};
State.prototype.getMedian = function () {
const sortedArray = this.array.slice().sort((a, b) => a - b);
if (sortedArray.length % 2 === 0) {
const _index = sortedArray.length / 2;
return Number(((this.array[_index] + this.array[_index - 1]) / 2).toFixed(2));
} else {
return this.array[Math.floor(sortedArray.length / 2)];
}
};
State.prototype.getMode = function () {
const obj = {};
const result = [];
let maxCount = 0;
this.array.forEach((ele) => {
if (!obj[ele]) {
obj[ele] = 1;
} else {
obj[ele]++;
}
});
for (let prop in obj) {
if (maxCount < obj[prop]) {
maxCount = obj[prop];
}
}
for (let prop in obj) {
if (obj[prop] === maxCount) result.push(Number(prop));
}
return result.length === Object.keys(obj).length ? [] : result;
};
const state1 = new State(array1);
console.log(state1.getMean()); //3.43
console.log(state1.getMedian()); //4
console.log(state1.getMode()); //[ 4, 5 ]
const state2 = new State(array2);
console.log(state2.getMean()); //2.5
console.log(state2.getMedian()); //2.5
console.log(state2.getMode()); //[]
})();