-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathaboveaverage.js
More file actions
58 lines (38 loc) · 952 Bytes
/
aboveaverage.js
File metadata and controls
58 lines (38 loc) · 952 Bytes
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
const readline = require('readline');
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout
});
let c;
rl.on('line', (line) => {
if(!c){
c = line;
}
else {
let arr = line.split(' ').map( x => parseInt(x) );
arr = arr.slice(1);
let avg = average(arr);
let abvAvg = aboveAverage(avg, arr);
let str = formatStr(abvAvg, 100, 3, '%')
console.log(str);
}
});
function formatStr(num, mult = 1, degree = 2, sign = '%'){
let str = parseFloat(num * mult).toFixed(degree) + sign;
return str;
}
function aboveAverage(avg, arr = []){
let count = 0;
arr.forEach( x => {
if( x > avg){
count ++;
}
});
let abvAvg = count / arr.length;
return abvAvg;
}
function average(arr = []){
let sum = arr.reduce( (a,b) => a + b, 0);
let avg = sum / arr.length;
return avg;
}