-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtask2.js
41 lines (39 loc) · 1.33 KB
/
task2.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
/*
* Given an array of countries with their population.
* Calculate average population of the countries.
*
* [
* { name: 'Ukraine', population: 42 000 000},
* { name: 'Belarus', population: 9 500 000},
* { name: 'Moldova', population: 3 500 000},
* { name: 'Switzerland', population: 8 400 000}
* ]
*
* Total population = 42M + 9.5M + 3.5M + 8.4M = 63.4M
*/
function calculateAverageCountryPopulation(countries) {
// Only change code below this line
function average( source, selector) {
let res = 0,
count = 0;
if (selector)
for (let v of source) {
let v2 = selector(v);
if (Number.isFinite(v2)) {
res += v2;
++count;
}
}
else
for (let v of source) {
if (Number.isFinite(v)) {
res += v;
++count;
}
}
if (count)
return res / count;
return 0;
}
return average(countries, e => e.population);
// Only change code above this line