-
Notifications
You must be signed in to change notification settings - Fork 86
/
Copy paths10-basic-statistics.js
40 lines (32 loc) · 974 Bytes
/
s10-basic-statistics.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
function processData(input) {
//Enter your code here
input = input.split('\n')[1].split(' ').map(Number)
frequency = []
input = input.sort((a, b) => {
if (a === b)
frequency.push(a)
return a - b
})
let mean = input.reduce((a, b) => a + b) / input.length
let median = 0
if (input.length % 2 === 0) { // even
median = (input[input.length / 2 - 1] + input[input.length / 2]) / 2
} else { // odd
median = input[(input.length - 1) / 2]
}
let mode = input[0]
if (frequency.length > 0) {
mode = frequency.sort((a, b) => {
let fa = frequency.filter(v => v === a).length
let fb = frequency.filter(v => v === b).length
if (fa === fb && a < b) {
return fa - fb + 1
}
return fa - fb
})
mode = mode.pop()
}
console.log(parseFloat(mean.toFixed(1)) + '\n' + parseFloat(median.toFixed(1)) + '\n' + mode)
}
processData(`10
64630 11735 14216 99233 14470 4978 73429 38120 51135 67060`)